mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
run lint
This commit is contained in:
parent
3e22e36a79
commit
7320711a36
23 changed files with 36 additions and 81 deletions
|
|
@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Ehesp\SteamLogin\SteamLogin;
|
||||
use Demostf\API\Providers\AuthProvider;
|
||||
use Demostf\API\Providers\UserProvider;
|
||||
use Ehesp\SteamLogin\SteamLogin;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class BaseController {
|
|||
}
|
||||
|
||||
protected function query($name, $default) {
|
||||
return isset($this->request->query[$name]) ? $this->request->query[$name] : $default;
|
||||
return $this->request->query[$name] ?? $default;
|
||||
}
|
||||
|
||||
protected function file($name) {
|
||||
|
|
@ -25,7 +25,7 @@ class BaseController {
|
|||
}
|
||||
|
||||
protected function post($name, $default = null) {
|
||||
return isset($this->request->data[$name]) ? $this->request->data[$name] : $default;
|
||||
return $this->request->data[$name] ?? $default;
|
||||
}
|
||||
|
||||
protected function json($data, $code = 200, $encode = true, $charset = 'utf-8', $option = 0) {
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ use Demostf\API\Providers\DemoListProvider;
|
|||
use Demostf\API\Providers\DemoProvider;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
use function intval;
|
||||
use function is_array;
|
||||
|
||||
class DemoController extends BaseController {
|
||||
/** @var DemoProvider */
|
||||
|
|
@ -49,7 +47,7 @@ class DemoController extends BaseController {
|
|||
* @param string $id
|
||||
*/
|
||||
public function get($id) {
|
||||
$this->json($this->demoProvider->get(intval($id, 10)));
|
||||
$this->json($this->demoProvider->get(\intval($id, 10)));
|
||||
}
|
||||
|
||||
protected function getFilter() {
|
||||
|
|
@ -67,7 +65,7 @@ class DemoController extends BaseController {
|
|||
$filter['backend'] = $backend;
|
||||
}
|
||||
if ($players) {
|
||||
if (!is_array($players)) {
|
||||
if (!\is_array($players)) {
|
||||
$players = explode(',', $players);
|
||||
}
|
||||
$players = array_filter($players);
|
||||
|
|
|
|||
|
|
@ -28,8 +28,9 @@ class UserController extends BaseController {
|
|||
$steamId = Parser::convertSteamIdToCommunityId($steamId);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
}
|
||||
} else if ($user = $this->userProvider->getById((int)$steamId)) {
|
||||
} elseif ($user = $this->userProvider->getById((int) $steamId)) {
|
||||
Flight::json($user);
|
||||
|
||||
return;
|
||||
}
|
||||
Flight::json($this->userProvider->get($steamId));
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@ class ParsedDemo {
|
|||
/**
|
||||
* ParsedDemo constructor.
|
||||
*
|
||||
* @param int $redScore
|
||||
* @param int $blueScore
|
||||
* @param ChatMessage[] $chat
|
||||
* @param ParsedPlayer[] $players
|
||||
* @param ParsedKill[] $kills
|
||||
|
|
|
|||
|
|
@ -18,10 +18,6 @@ class ChatMessage implements JsonSerializable {
|
|||
|
||||
/**
|
||||
* ChatMessage constructor.
|
||||
*
|
||||
* @param string $user
|
||||
* @param int $time
|
||||
* @param string $message
|
||||
*/
|
||||
public function __construct(string $user, int $time, string $message) {
|
||||
$this->user = $user;
|
||||
|
|
@ -29,23 +25,14 @@ class ChatMessage implements JsonSerializable {
|
|||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUser(): string {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTime(): int {
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage(): string {
|
||||
return $this->message;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Demo;
|
||||
|
||||
use function count;
|
||||
use DateTime;
|
||||
use Demostf\API\Data\Kill;
|
||||
use Demostf\API\Data\ParsedDemo;
|
||||
|
|
@ -66,7 +65,7 @@ class DemoSaver {
|
|||
$upload->getBlue(),
|
||||
$demo->getRedScore(),
|
||||
$demo->getBlueScore(),
|
||||
count($demo->getPlayers()),
|
||||
\count($demo->getPlayers()),
|
||||
$upload->getUploaderId(),
|
||||
$upload->getHash(),
|
||||
$storedDemo->getBackend(),
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||
namespace Demostf\API\Demo;
|
||||
|
||||
use Demostf\API\Data\StoredDemo;
|
||||
use function dirname;
|
||||
|
||||
class DemoStore {
|
||||
/** @var string */
|
||||
|
|
@ -20,8 +19,8 @@ class DemoStore {
|
|||
|
||||
public function store(string $sourcePath, string $name): StoredDemo {
|
||||
$target = $this->generatePath($name);
|
||||
if (!is_dir(dirname($target))) {
|
||||
mkdir(dirname($target), 0777, true);
|
||||
if (!is_dir(\dirname($target))) {
|
||||
mkdir(\dirname($target), 0777, true);
|
||||
}
|
||||
rename($sourcePath, $target);
|
||||
chmod($target, 0755);
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ class HeaderParser {
|
|||
* @param string $head string containing the demo header binary data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseString(string $head): Header {
|
||||
$info = @unpack(
|
||||
|
|
@ -32,8 +30,6 @@ class HeaderParser {
|
|||
* @param resource $stream
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseStream($stream): Header {
|
||||
$head = fread($stream, 2048);
|
||||
|
|
@ -44,11 +40,7 @@ class HeaderParser {
|
|||
/**
|
||||
* Parse demo info from a local file.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return Header
|
||||
*/
|
||||
public function parseHeader(string $path): Header {
|
||||
if (!is_readable($path)) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ use Demostf\API\Data\ParsedKill;
|
|||
use Demostf\API\Data\ParsedPlayer;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Higher level parser.
|
||||
|
|
@ -38,7 +37,7 @@ class Parser {
|
|||
|
||||
public function analyse(string $path): ParsedDemo {
|
||||
$data = $this->rawParser->parse($path);
|
||||
if (is_array($data) && isset($data['intervalPerTick'])) {
|
||||
if (\is_array($data) && isset($data['intervalPerTick'])) {
|
||||
return $this->handleData($data);
|
||||
} else {
|
||||
throw new InvalidArgumentException('Error parsing demo');
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@ class RawParser {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function parse(string $path): ?array {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
@ -18,11 +17,9 @@ declare(strict_types=1);
|
|||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Demostf\API\Error;
|
||||
|
||||
class InvalidHashException extends \Exception {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
@ -18,11 +17,9 @@ declare(strict_types=1);
|
|||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Demostf\API\Error;
|
||||
|
||||
class InvalidKeyException extends \Exception {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Providers;
|
||||
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
use function count;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use function is_array;
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
use PDO;
|
||||
|
||||
class DemoListProvider extends BaseProvider {
|
||||
|
|
@ -41,13 +39,13 @@ class DemoListProvider extends BaseProvider {
|
|||
$query->select('p.demo_id')
|
||||
->from('players', 'p');
|
||||
|
||||
if (count($userIds) > 1) {
|
||||
if (\count($userIds) > 1) {
|
||||
$query->where($query->expr()->in('user_id',
|
||||
$query->createNamedParameter($userIds, Connection::PARAM_INT_ARRAY)))
|
||||
->groupBy('demo_id')
|
||||
->having($query->expr()->eq(
|
||||
'COUNT(user_id)',
|
||||
$query->createNamedParameter(count($userIds), PDO::PARAM_INT)
|
||||
$query->createNamedParameter(\count($userIds), PDO::PARAM_INT)
|
||||
));
|
||||
} else {
|
||||
$query->where($query->expr()->eq('user_id',
|
||||
|
|
@ -57,7 +55,7 @@ class DemoListProvider extends BaseProvider {
|
|||
->setMaxResults(50)
|
||||
->setFirstResult(((int) $page - 1) * 50);
|
||||
|
||||
if (count($where)) {
|
||||
if (\count($where)) {
|
||||
$query->innerJoin('p', 'demos', 'd', $query->expr()->eq('demo_id', 'd.id'));
|
||||
$this->addWhere($query, $where);
|
||||
}
|
||||
|
|
@ -105,14 +103,10 @@ class DemoListProvider extends BaseProvider {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @param array $where
|
||||
* @param string $order
|
||||
*
|
||||
* @return Demo[]
|
||||
*/
|
||||
public function listDemos(int $page, array $where = [], string $order = 'DESC') {
|
||||
if (isset($where['players']) and is_array($where['players']) and count($where['players']) > 0) {
|
||||
if (isset($where['players']) and \is_array($where['players']) and \count($where['players']) > 0) {
|
||||
return $this->listProfile($page, $where);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Providers;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use const DATE_ATOM;
|
||||
use Demostf\API\Data\DemoPlayer;
|
||||
use Demostf\API\Demo\Demo;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use PDO;
|
||||
|
||||
class DemoProvider extends BaseProvider {
|
||||
|
|
@ -43,7 +43,7 @@ class DemoProvider extends BaseProvider {
|
|||
WHERE demo_id = ?';
|
||||
|
||||
$demo = $this->fetchDemo($id);
|
||||
if ($demo === null) {
|
||||
if (null === $demo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class InfoProvider extends BaseProvider {
|
|||
$query = $this->getQueryBuilder();
|
||||
$query->select('count(*)')
|
||||
->from($table);
|
||||
|
||||
return $query->execute()->fetch(PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ namespace Demostf\API\Providers;
|
|||
use Demostf\API\Data\SteamUser;
|
||||
use Demostf\API\Data\User;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use function is_array;
|
||||
use PDO;
|
||||
use RandomLib\Generator;
|
||||
use SteamId;
|
||||
|
|
@ -108,7 +107,7 @@ class UserProvider extends BaseProvider {
|
|||
->setMaxResults(1);
|
||||
|
||||
$result = $query->execute()->fetch();
|
||||
if (is_array($result)) {
|
||||
if (\is_array($result)) {
|
||||
return $result;
|
||||
} else {
|
||||
return null;
|
||||
|
|
@ -116,8 +115,6 @@ class UserProvider extends BaseProvider {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $search
|
||||
*
|
||||
* @return SteamUser[]
|
||||
*/
|
||||
public function search(string $search): array {
|
||||
|
|
|
|||
|
|
@ -68,11 +68,11 @@ Flight::route('/auth/handle/@token', [$authController, 'handle']);
|
|||
Flight::route('/auth/login/@token', [$authController, 'login']);
|
||||
Flight::route('/auth/logout/@token', [$authController, 'logout']);
|
||||
|
||||
Flight::map('error', function(\Exception $ex){
|
||||
Flight::map('error', function (\Exception $ex) {
|
||||
$code = 500;
|
||||
if ($ex instanceof InvalidKeyException) {
|
||||
$code = 401;
|
||||
} else if ($ex instanceof InvalidHashException) {
|
||||
} elseif ($ex instanceof InvalidHashException) {
|
||||
$code = 412;
|
||||
}
|
||||
Flight::response()->status($code)->write($ex->getMessage())->send();
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use flight\net\Response;
|
|||
use flight\util\Collection;
|
||||
|
||||
abstract class ControllerTest extends TestCase {
|
||||
/** @var string $responseData */
|
||||
/** @var string */
|
||||
private $responseData;
|
||||
|
||||
protected function getRequest(array $get = [], array $post = [], array $files = []): Request {
|
||||
|
|
|
|||
|
|
@ -13,17 +13,16 @@ use Demostf\API\Error\InvalidKeyException;
|
|||
use Demostf\API\Providers\ChatProvider;
|
||||
use Demostf\API\Providers\DemoListProvider;
|
||||
use Demostf\API\Providers\DemoProvider;
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
class DemoControllerTest extends ControllerTest {
|
||||
/** @var DemoStore|MockObject $demoStore */
|
||||
/** @var DemoStore|MockObject */
|
||||
private $demoStore;
|
||||
/** @var DemoProvider|MockObject $demoProvider */
|
||||
/** @var DemoProvider|MockObject */
|
||||
private $demoProvider;
|
||||
/** @var ChatProvider|MockObject $chatProvider */
|
||||
/** @var ChatProvider|MockObject */
|
||||
private $chatProvider;
|
||||
/** @var DemoListProvider|MockObject $demoListProvider */
|
||||
/** @var DemoListProvider|MockObject */
|
||||
private $demoListProvider;
|
||||
|
||||
public function setUp(): void {
|
||||
|
|
@ -208,7 +207,7 @@ class DemoControllerTest extends ControllerTest {
|
|||
->method('listDemos')
|
||||
->with(1, [
|
||||
'before' => \DateTime::createFromFormat('U', '500'),
|
||||
'after' => \DateTime::createFromFormat('U', '100')
|
||||
'after' => \DateTime::createFromFormat('U', '100'),
|
||||
], 'DESC')
|
||||
->willReturn(['dummy']);
|
||||
|
||||
|
|
@ -222,7 +221,7 @@ class DemoControllerTest extends ControllerTest {
|
|||
$this->demoListProvider->expects($this->once())
|
||||
->method('listDemos')
|
||||
->with(1, [
|
||||
'before' => \DateTime::createFromFormat('U', '500')
|
||||
'before' => \DateTime::createFromFormat('U', '500'),
|
||||
], 'DESC')
|
||||
->willReturn(['dummy']);
|
||||
|
||||
|
|
|
|||
|
|
@ -23,11 +23,11 @@ class ParserTest extends TestCase {
|
|||
|
||||
$this->rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->will($this->returnCallback(function ($path) {
|
||||
->willReturnCallback(function ($path) {
|
||||
$jsonPath = str_replace('.dem', '-raw.json', $path);
|
||||
|
||||
return json_decode(file_get_contents($jsonPath), true);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
public function testAnalyse() {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class DemoListProviderTest extends TestCase {
|
|||
}
|
||||
|
||||
private function getDemo(int $uploaderId, $map = 'map', $playerCount = 18, int $time = null) {
|
||||
$time = is_null($time) ? new \DateTime() : \DateTime::createFromFormat('U', (string) $time);
|
||||
$time = null === $time ? new \DateTime() : \DateTime::createFromFormat('U', (string) $time);
|
||||
|
||||
return new Demo(
|
||||
0,
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ use InvalidArgumentException;
|
|||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use ReflectionException;
|
||||
use function strlen;
|
||||
|
||||
class UploadProviderTest extends TestCase {
|
||||
/** @var RawParser */
|
||||
|
|
@ -59,11 +58,11 @@ class UploadProviderTest extends TestCase {
|
|||
|
||||
$this->rawParser->expects($this->any())
|
||||
->method('parse')
|
||||
->will($this->returnCallback(function ($path) {
|
||||
->willReturnCallback(function ($path) {
|
||||
$jsonPath = str_replace('.dem', '-raw.json', $path);
|
||||
|
||||
return json_decode(file_get_contents($jsonPath), true);
|
||||
}));
|
||||
});
|
||||
|
||||
$targetDir = tempnam(sys_get_temp_dir(), 'dummy_target_');
|
||||
unlink($targetDir);
|
||||
|
|
@ -307,7 +306,7 @@ class UploadProviderTest extends TestCase {
|
|||
$result = $this->uploadProvider->upload($token, 'RED', 'BLU', 'foodemo', $this->tmpDir . '/foo.dem');
|
||||
$this->assertStringStartsWith('STV available at: http://example.com/', $result);
|
||||
|
||||
$demoId = (int) substr($result, strlen('STV available at: http://example.com/'));
|
||||
$demoId = (int) substr($result, \strlen('STV available at: http://example.com/'));
|
||||
|
||||
$demo = $this->demoProvider->get($demoId, true);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue