From 47d1205671b17f4d5cef1aecb0c25c360e95e72f Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 18:44:53 +0900 Subject: [PATCH 01/11] Drop PHP 7.x support --- .github/workflows/continuous-integration.yml | 2 +- composer.json | 2 +- phpcs.xml | 44 ++++++++++++-------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 52c828fe..b750635d 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -9,5 +9,5 @@ jobs: ci: uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1 with: - old_stable: '["7.4", "8.0", "8.1"]' + old_stable: '["8.0", "8.1"]' current_stable: 8.2 diff --git a/composer.json b/composer.json index c3ead671..75fc0138 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": "^7.3 || ^8.0", + "php": "^8.0", "ext-json": "*", "ext-pdo": "*", "aura/sql": "^4.0 || ^5.0", diff --git a/phpcs.xml b/phpcs.xml index dd74c95d..1d625999 100755 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,15 +1,15 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> - - + + @@ -22,6 +22,17 @@ + + + */src/*Interface.php + */src/Abstract*.php + */src/ResourceObject.php + + + */src/*Interface.php + */src/Abstract*.php + */src/ResourceObject.php + @@ -33,27 +44,20 @@ - - - - - - - + + - + + + */tests/Fake/* - + - - - - - + @@ -75,11 +79,15 @@ + + src/ClassParam.php + + */Fake/* */tmp/* From 410c268538e36afd76223cf34cd46b45afa16f6d Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 18:47:34 +0900 Subject: [PATCH 02/11] Update doctrine coding standard v10 --- composer.json | 2 +- phpcs.xml | 1 + src/Pages.php | 12 +++--------- tests/DbQueryModuleTest.php | 11 +++-------- tests/ParamInjectorTest.php | 3 +-- tests/SqlQueryTest.php | 9 +++------ tests/WebQueryModuleTest.php | 11 +++-------- 7 files changed, 15 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index 75fc0138..4c6180f3 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.5.5", - "doctrine/coding-standard": "^9", + "doctrine/coding-standard": "^10.0", "phpmd/phpmd": "^2.9", "phpmetrics/phpmetrics": "^2.7", "phpstan/phpstan": "^1.7", diff --git a/phpcs.xml b/phpcs.xml index 1d625999..a2618415 100755 --- a/phpcs.xml +++ b/phpcs.xml @@ -42,6 +42,7 @@ + diff --git a/src/Pages.php b/src/Pages.php index 8ca9ac8f..448f05ef 100644 --- a/src/Pages.php +++ b/src/Pages.php @@ -35,18 +35,12 @@ public function __construct(AuraSqlPagerInterface $pager, ExtendedPdoInterface $ $this->params = $values; } - /** - * @param int $pageIndex - */ - public function offsetExists($pageIndex): bool + public function offsetExists(int $pageIndex): bool { return (bool) $this->offsetGet($pageIndex); } - /** - * @param int $pageIndex - */ - public function offsetGet($pageIndex): ?Page + public function offsetGet(int $pageIndex): ?Page { return $this->delegate->offsetGet($pageIndex); } @@ -57,7 +51,7 @@ public function offsetGet($pageIndex): ?Page * * @codeCoverageIgnore */ - public function offsetSet($offset, $value): void + public function offsetSet(int $offset, $value): void { unset($offset, $value); diff --git a/tests/DbQueryModuleTest.php b/tests/DbQueryModuleTest.php index 9905f3a0..1f52cfab 100644 --- a/tests/DbQueryModuleTest.php +++ b/tests/DbQueryModuleTest.php @@ -36,14 +36,9 @@ class DbQueryModuleTest extends TestCase { - /** @var AbstractModule */ - protected $module; - - /** @var MediaQueryLoggerInterface */ - private $logger; - - /** @var Injector */ - private $injector; + protected AbstractModule $module; + private MediaQueryLoggerInterface $logger; + private Injector $injector; protected function setUp(): void { diff --git a/tests/ParamInjectorTest.php b/tests/ParamInjectorTest.php index 78eb385a..dac07bb4 100644 --- a/tests/ParamInjectorTest.php +++ b/tests/ParamInjectorTest.php @@ -13,8 +13,7 @@ class ParamInjectorTest extends TestCase { - /** @var ParamInjector */ - private $injector; + private ParamInjector $injector; protected function setUp(): void { diff --git a/tests/SqlQueryTest.php b/tests/SqlQueryTest.php index f2fdb91f..730f433d 100644 --- a/tests/SqlQueryTest.php +++ b/tests/SqlQueryTest.php @@ -23,14 +23,11 @@ class SqlQueryTest extends TestCase { - /** @var SqlQuery */ - private $sqlQuery; - - /** @var MediaQueryLogger */ - private $log; + private SqlQuery $sqlQuery; + private MediaQueryLogger $log; /** @var array */ - private $insertData = ['id' => '1', 'title' => 'run']; + private array $insertData = ['id' => '1', 'title' => 'run']; protected function setUp(): void { diff --git a/tests/WebQueryModuleTest.php b/tests/WebQueryModuleTest.php index 0b6c944f..63f1e70e 100644 --- a/tests/WebQueryModuleTest.php +++ b/tests/WebQueryModuleTest.php @@ -14,14 +14,9 @@ class WebQueryModuleTest extends TestCase { - /** @var AbstractModule */ - protected $module; - - /** @var MediaQueryLoggerInterface */ - private $logger; - - /** @var Injector */ - private $injector; + protected AbstractModule $module; + private MediaQueryLoggerInterface $logger; + private Injector $injector; protected function setUp(): void { From 96a05be1398c2c70888c1e04417be33ed12960c6 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 18:51:16 +0900 Subject: [PATCH 03/11] Soothe phpstan --- src/Pages.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Pages.php b/src/Pages.php index 448f05ef..354e0fb3 100644 --- a/src/Pages.php +++ b/src/Pages.php @@ -35,12 +35,12 @@ public function __construct(AuraSqlPagerInterface $pager, ExtendedPdoInterface $ $this->params = $values; } - public function offsetExists(int $pageIndex): bool + public function offsetExists($pageIndex): bool { return (bool) $this->offsetGet($pageIndex); } - public function offsetGet(int $pageIndex): ?Page + public function offsetGet($pageIndex): ?Page { return $this->delegate->offsetGet($pageIndex); } @@ -51,7 +51,7 @@ public function offsetGet(int $pageIndex): ?Page * * @codeCoverageIgnore */ - public function offsetSet(int $offset, $value): void + public function offsetSet($offset, $value): void { unset($offset, $value); From 60cfdc59b7f9ab5917450355a4b9099c9302701c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:20:31 +0900 Subject: [PATCH 04/11] Rector --- composer.json | 4 +- phpcs.xml | 12 +--- src/Annotation/DbQuery.php | 32 +++------- src/Annotation/Pager.php | 24 ++------ src/Annotation/Qualifier/SqlDir.php | 13 ++++ .../Qualifier/UriTemplateBindings.php | 16 +---- src/Annotation/Qualifier/WebApiList.php | 16 +---- src/Annotation/SqlDir.php | 18 ------ src/Annotation/WebQuery.php | 15 +---- src/CamelCaseTrait.php | 5 +- src/ClassesInDirectories.php | 2 +- src/DbQueryConfig.php | 9 +-- src/DbQueryInterceptor.php | 37 ++++-------- src/MediaQueryBaseModule.php | 10 ++-- src/MediaQueryDbModule.php | 14 ++--- src/MediaQueryLogger.php | 7 ++- src/MediaQueryLoggerInterface.php | 4 +- src/MediaQueryModule.php | 19 ++---- src/MediaQueryWebModule.php | 12 ++-- src/Pages.php | 43 ++++--------- src/PagesInterface.php | 4 +- src/ParamConverter.php | 3 +- src/ParamConverterInterface.php | 4 +- src/ParamInjector.php | 9 +-- src/ParamInjectorInterface.php | 4 +- src/Queries.php | 21 ++----- src/SqlQuery.php | 60 +++++-------------- src/SqlQueryFactory.php | 10 ++-- src/SqlQueryInterface.php | 15 ++--- src/ToScalarInterface.php | 5 +- src/WebApiQuery.php | 30 +++------- src/WebQueryConfig.php | 18 +++--- src/WebQueryInterceptor.php | 32 +++------- tests/Fake/Entity/TodoConstruct.php | 15 ++--- tests/Fake/Explicit/TodoAdd.php | 9 +-- tests/Fake/Explicit/TodoItem.php | 9 +-- tests/Fake/Explicit/TodoList.php | 9 +-- tests/Fake/FakeFoo.php | 14 ++--- tests/Fake/FakeString.php | 2 +- .../Fake/Queries/DynamicPerPageInterface.php | 16 ----- .../DynamicPerPageInvalidInterface.php | 4 -- .../Queries/DynamicPerPageInvalidType.php | 4 -- tests/Fake/Queries/PagerEntityInterface.php | 4 -- tests/Fake/Queries/PromiseAddInterface.php | 3 - tests/Fake/Queries/PromiseItemInterface.php | 4 -- tests/Fake/Queries/PromiseListInterface.php | 4 -- tests/Fake/Queries/TodoAddInterface.php | 4 -- .../Queries/TodoConstcuctEntityInterface.php | 6 -- tests/Fake/Queries/TodoEntityInterface.php | 7 --- tests/Fake/Queries/TodoItemInterface.php | 4 -- tests/Fake/Queries/TodoListInterface.php | 4 -- tests/Fake/WebApi/FooItemInterface.php | 3 - tests/FakeParamInjectMethod.php | 4 +- tests/SqlQueryTest.php | 34 +++-------- 54 files changed, 184 insertions(+), 506 deletions(-) create mode 100644 src/Annotation/Qualifier/SqlDir.php delete mode 100644 src/Annotation/SqlDir.php diff --git a/composer.json b/composer.json index 4c6180f3..308d2103 100644 --- a/composer.json +++ b/composer.json @@ -25,11 +25,13 @@ "nikic/php-parser": "^4.15" }, "require-dev": { - "phpunit/phpunit": "^9.5.5", "doctrine/coding-standard": "^10.0", "phpmd/phpmd": "^2.9", "phpmetrics/phpmetrics": "^2.7", "phpstan/phpstan": "^1.7", + "phpunit/phpunit": "^9.5.5", + "ray/rector-ray": "^1.0", + "rector/rector": "^0.14.8", "squizlabs/php_codesniffer": "^3.7", "vimeo/psalm": "^4.24" }, diff --git a/phpcs.xml b/phpcs.xml index a2618415..4d7b5f79 100755 --- a/phpcs.xml +++ b/phpcs.xml @@ -23,16 +23,8 @@ - - */src/*Interface.php - */src/Abstract*.php - */src/ResourceObject.php - - - */src/*Interface.php - */src/Abstract*.php - */src/ResourceObject.php - + + diff --git a/src/Annotation/DbQuery.php b/src/Annotation/DbQuery.php index 3cfe7a8a..51363f36 100644 --- a/src/Annotation/DbQuery.php +++ b/src/Annotation/DbQuery.php @@ -5,10 +5,6 @@ namespace Ray\MediaQuery\Annotation; use Attribute; -use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; - -use function assert; -use function class_exists; /** * @Annotation @@ -18,26 +14,12 @@ #[Attribute(Attribute::TARGET_METHOD)] final class DbQuery { - /** @var string */ - public $id; - - /** @var ?class-string */ - public $entity; - - /** - * @Enum({"row", "row_list"}) - * @var 'row'|'row_list' - */ - public $type = 'row_list'; - - /** - * @param 'row'|'row_list' $type - */ - public function __construct(string $id, string $type = 'row_list', ?string $entity = null) - { - assert($entity === null || class_exists($entity)); - $this->id = $id; - $this->entity = $entity; - $this->type = $type; + /** @param 'row'|'row_list' $type */ + public function __construct( + public string $id, + public string $type = 'row_list', + /** @var ?class-string */ + public string|null $entity = null, + ) { } } diff --git a/src/Annotation/Pager.php b/src/Annotation/Pager.php index d3b771c9..b52aa0b5 100644 --- a/src/Annotation/Pager.php +++ b/src/Annotation/Pager.php @@ -5,28 +5,14 @@ namespace Ray\MediaQuery\Annotation; use Attribute; -use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; -/** - * @Annotation - * @Target("METHOD") - * @NamedArgumentConstructor - */ #[Attribute(Attribute::TARGET_METHOD)] final class Pager { - /** @var int|string */ - public $perPage; - - /** @var string */ - public $template; - - /** - * @param int|string $perPage int:the number of items, string: the name of the argument of the number of items - */ - public function __construct($perPage = 10, string $template = '/{?page}') - { - $this->perPage = $perPage; - $this->template = $template; + /** @param int|string $perPage int:the number of items, string: the name of the argument of the number of items */ + public function __construct( + public int|string $perPage = 10, + public string $template = '/{?page}', + ) { } } diff --git a/src/Annotation/Qualifier/SqlDir.php b/src/Annotation/Qualifier/SqlDir.php new file mode 100644 index 00000000..abc21167 --- /dev/null +++ b/src/Annotation/Qualifier/SqlDir.php @@ -0,0 +1,13 @@ +value = $value; - } } diff --git a/src/Annotation/Qualifier/WebApiList.php b/src/Annotation/Qualifier/WebApiList.php index 2e065252..afba4c4e 100644 --- a/src/Annotation/Qualifier/WebApiList.php +++ b/src/Annotation/Qualifier/WebApiList.php @@ -5,23 +5,9 @@ namespace Ray\MediaQuery\Annotation\Qualifier; use Attribute; -use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; use Ray\Di\Di\Qualifier; -/** - * @Annotation - * @Target("METHOD") - * @Qualifier - * @NamedArgumentConstructor - */ -#[Attribute(Attribute::TARGET_METHOD), Qualifier] +#[Attribute(Attribute::TARGET_PARAMETER), Qualifier] final class WebApiList { - /** @var string */ - public $value; - - public function __construct(string $value) - { - $this->value = $value; - } } diff --git a/src/Annotation/SqlDir.php b/src/Annotation/SqlDir.php deleted file mode 100644 index b76defce..00000000 --- a/src/Annotation/SqlDir.php +++ /dev/null @@ -1,18 +0,0 @@ -id = $id; + public function __construct( + public string $id, + ) { } } diff --git a/src/CamelCaseTrait.php b/src/CamelCaseTrait.php index 508a27a1..46cf8838 100644 --- a/src/CamelCaseTrait.php +++ b/src/CamelCaseTrait.php @@ -10,10 +10,7 @@ trait CamelCaseTrait { - /** - * @param mixed $value - */ - public function __set(string $name, $value): void + public function __set(string $name, mixed $value): void { $propName = lcfirst(str_replace('_', '', ucwords($name, '_'))); $this->{$propName} = $value; diff --git a/src/ClassesInDirectories.php b/src/ClassesInDirectories.php index 46e9151d..575fcaeb 100644 --- a/src/ClassesInDirectories.php +++ b/src/ClassesInDirectories.php @@ -42,7 +42,7 @@ public static function list(string ...$directories): iterable $sourceLocator = new AggregateSourceLocator([ new DirectoriesSourceLocator( $directories, - (new BetterReflection())->astLocator() + (new BetterReflection())->astLocator(), ), // ↓ required to autoload parent classes/interface from another directory than /src (e.g. /vendor) new AutoloadSourceLocator((new BetterReflection())->astLocator()), diff --git a/src/DbQueryConfig.php b/src/DbQueryConfig.php index cf76d325..197fe987 100644 --- a/src/DbQueryConfig.php +++ b/src/DbQueryConfig.php @@ -6,11 +6,8 @@ final class DbQueryConfig { - /** @var string */ - public $sqlDir; - - public function __construct(string $sqlDir) - { - $this->sqlDir = $sqlDir; + public function __construct( + public string $sqlDir, + ) { } } diff --git a/src/DbQueryInterceptor.php b/src/DbQueryInterceptor.php index d245cb49..d434dc78 100644 --- a/src/DbQueryInterceptor.php +++ b/src/DbQueryInterceptor.php @@ -20,26 +20,15 @@ class DbQueryInterceptor implements MethodInterceptor { - /** @var SqlQueryInterface */ - private $sqlQuery; - - /** @var MediaQueryLoggerInterface */ - private $logger; - - /** @var ParamInjectorInterface */ - private $paramInjector; - - public function __construct(SqlQueryInterface $sqlQuery, MediaQueryLoggerInterface $logger, ParamInjectorInterface $paramInjector) - { - $this->sqlQuery = $sqlQuery; - $this->logger = $logger; - $this->paramInjector = $paramInjector; + public function __construct( + private SqlQueryInterface $sqlQuery, + private MediaQueryLoggerInterface $logger, + private ParamInjectorInterface $paramInjector, + ) { } - /** - * @return array|object|PagesInterface|null - */ - public function invoke(MethodInvocation $invocation) + /** @return array|object|null */ + public function invoke(MethodInvocation $invocation): array|object|null { $method = $invocation->getMethod(); /** @var DbQuery $dbQuery */ @@ -55,9 +44,7 @@ public function invoke(MethodInvocation $invocation) return $this->sqlQuery($dbQuery, $values, $fetchStyle, (string) $dbQuery->entity); } - /** - * @return PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchStyle - */ + /** @return PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchStyle */ private function getFetchMode(DbQuery $dbQuery): int { if (! class_exists((string) $dbQuery->entity)) { @@ -78,7 +65,7 @@ private function getFetchMode(DbQuery $dbQuery): int * * @return array|object|null */ - private function sqlQuery(DbQuery $dbQuery, array $values, int $fetchStyle, $fetchArg) + private function sqlQuery(DbQuery $dbQuery, array $values, int $fetchStyle, int|string|callable $fetchArg): array|object|null { if ($dbQuery->type === 'row') { return $this->sqlQuery->getRow($dbQuery->id, $values, $fetchStyle, $fetchArg); @@ -87,10 +74,8 @@ private function sqlQuery(DbQuery $dbQuery, array $values, int $fetchStyle, $fet return $this->sqlQuery->getRowList($dbQuery->id, $values, $fetchStyle, $fetchArg); } - /** - * @param array $values - */ - private function getPager(string $queryId, array $values, Pager $pager, ?string $entity): PagesInterface + /** @param array $values */ + private function getPager(string $queryId, array $values, Pager $pager, string|null $entity): PagesInterface { if (is_string($pager->perPage)) { $values = $this->getDynamicPerPage($pager, $values); diff --git a/src/MediaQueryBaseModule.php b/src/MediaQueryBaseModule.php index dfc59f91..1d259746 100644 --- a/src/MediaQueryBaseModule.php +++ b/src/MediaQueryBaseModule.php @@ -11,12 +11,10 @@ class MediaQueryBaseModule extends AbstractModule { - /** @var Queries */ - private $queries; - - public function __construct(Queries $queries, ?AbstractModule $module = null) - { - $this->queries = $queries; + public function __construct( + private Queries $queries, + AbstractModule|null $module = null, + ) { parent::__construct($module); } diff --git a/src/MediaQueryDbModule.php b/src/MediaQueryDbModule.php index d884ea88..71b15fd7 100644 --- a/src/MediaQueryDbModule.php +++ b/src/MediaQueryDbModule.php @@ -6,16 +6,14 @@ use Ray\Di\AbstractModule; use Ray\MediaQuery\Annotation\DbQuery; -use Ray\MediaQuery\Annotation\SqlDir; +use Ray\MediaQuery\Annotation\Qualifier\SqlDir; class MediaQueryDbModule extends AbstractModule { - /** @var DbQueryConfig */ - private $configs; - - public function __construct(DbQueryConfig $config, ?AbstractModule $module = null) - { - $this->configs = $config; + public function __construct( + private DbQueryConfig $configs, + AbstractModule|null $module = null, + ) { parent::__construct($module); } @@ -25,7 +23,7 @@ protected function configure(): void $this->bindInterceptor( $this->matcher->any(), $this->matcher->annotatedWith(DbQuery::class), - [DbQueryInterceptor::class] + [DbQueryInterceptor::class], ); $this->bind()->annotatedWith(SqlDir::class)->toInstance($this->configs->sqlDir); } diff --git a/src/MediaQueryLogger.php b/src/MediaQueryLogger.php index fb4e6922..515520b2 100644 --- a/src/MediaQueryLogger.php +++ b/src/MediaQueryLogger.php @@ -4,13 +4,16 @@ namespace Ray\MediaQuery; +use Stringable; + use function implode; use function json_encode; use function sprintf; +use const JSON_THROW_ON_ERROR; use const PHP_EOL; -final class MediaQueryLogger implements MediaQueryLoggerInterface +final class MediaQueryLogger implements MediaQueryLoggerInterface, Stringable { /** @var list */ public $logs = []; @@ -24,7 +27,7 @@ public function start(): void */ public function log(string $queryId, array $values): void { - $this->logs[] = sprintf('query: %s(%s)', $queryId, json_encode($values)); + $this->logs[] = sprintf('query: %s(%s)', $queryId, json_encode($values, JSON_THROW_ON_ERROR)); } public function __toString(): string diff --git a/src/MediaQueryLoggerInterface.php b/src/MediaQueryLoggerInterface.php index 0d4f8110..960b25ce 100644 --- a/src/MediaQueryLoggerInterface.php +++ b/src/MediaQueryLoggerInterface.php @@ -8,9 +8,7 @@ interface MediaQueryLoggerInterface { public function start(): void; - /** - * @param array $values - */ + /** @param array $values */ public function log(string $queryId, array $values): void; public function __toString(): string; diff --git a/src/MediaQueryModule.php b/src/MediaQueryModule.php index 0361db9a..46c0c369 100644 --- a/src/MediaQueryModule.php +++ b/src/MediaQueryModule.php @@ -8,19 +8,12 @@ class MediaQueryModule extends AbstractModule { - /** @var Queries */ - private $queries; - - /** @var list */ - private $configs; - - /** - * @param list $configs - */ - public function __construct(Queries $queries, array $configs, ?AbstractModule $module = null) - { - $this->queries = $queries; - $this->configs = $configs; + /** @param list $configs */ + public function __construct( + private Queries $queries, + private array $configs, + AbstractModule|null $module = null, + ) { parent::__construct($module); } diff --git a/src/MediaQueryWebModule.php b/src/MediaQueryWebModule.php index 5d846fc6..e17447de 100644 --- a/src/MediaQueryWebModule.php +++ b/src/MediaQueryWebModule.php @@ -13,12 +13,10 @@ class MediaQueryWebModule extends AbstractModule { - /** @var WebQueryConfig */ - private $config; - - public function __construct(WebQueryConfig $config, ?AbstractModule $module = null) - { - $this->config = $config; + public function __construct( + private WebQueryConfig $config, + AbstractModule|null $module = null, + ) { parent::__construct($module); } @@ -27,7 +25,7 @@ public function configure(): void $this->bindInterceptor( $this->matcher->any(), $this->matcher->annotatedWith(WebQuery::class), - [WebQueryInterceptor::class] + [WebQueryInterceptor::class], ); $this->bind(ClientInterface::class)->to(Client::class); $this->bind(WebApiQueryInterface::class)->to(WebApiQuery::class); diff --git a/src/Pages.php b/src/Pages.php index 354e0fb3..1cef20ce 100644 --- a/src/Pages.php +++ b/src/Pages.php @@ -12,27 +12,13 @@ class Pages implements PagesInterface { - /** @var AuraSqlPagerInterface */ - private $delegate; - - /** @var ExtendedPdoInterface */ - private $pdo; - - /** @var string */ - private $sql; - - /** @var array */ - private $params; - - /** - * @param array $values - */ - public function __construct(AuraSqlPagerInterface $pager, ExtendedPdoInterface $pdo, string $sql, array $values) - { - $this->delegate = $pager; - $this->pdo = $pdo; - $this->sql = $sql; - $this->params = $values; + /** @param array $params */ + public function __construct( + private AuraSqlPagerInterface $delegate, + private ExtendedPdoInterface $pdo, + private string $sql, + private array $params, + ) { } public function offsetExists($pageIndex): bool @@ -40,30 +26,25 @@ public function offsetExists($pageIndex): bool return (bool) $this->offsetGet($pageIndex); } - public function offsetGet($pageIndex): ?Page + public function offsetGet($pageIndex): Page|null { return $this->delegate->offsetGet($pageIndex); } /** - * @param int $offset - * @param mixed $value + * @param int $offset * * @codeCoverageIgnore */ - public function offsetSet($offset, $value): void + public function offsetSet(mixed $offset, mixed $value): void { unset($offset, $value); throw new LogicException('Read only'); } - /** - * @param mixed $offset - * - * @codeCoverageIgnore - */ - public function offsetUnset($offset): void + /** @codeCoverageIgnore */ + public function offsetUnset(mixed $offset): void { unset($offset); diff --git a/src/PagesInterface.php b/src/PagesInterface.php index 3a9bf732..3716bc4b 100644 --- a/src/PagesInterface.php +++ b/src/PagesInterface.php @@ -7,9 +7,7 @@ use ArrayAccess; use Countable; -/** - * @extends ArrayAccess - */ +/** @extends ArrayAccess */ interface PagesInterface extends ArrayAccess, Countable { } diff --git a/src/ParamConverter.php b/src/ParamConverter.php index 8c955f1d..568872e6 100644 --- a/src/ParamConverter.php +++ b/src/ParamConverter.php @@ -10,7 +10,6 @@ use function assert; use function enum_exists; use function function_exists; -use function get_class; use function is_object; use function method_exists; use function print_r; @@ -46,7 +45,7 @@ public function __invoke(array &$values): void continue; } - if (function_exists('enum_exists') && enum_exists(get_class($value))) { + if (function_exists('enum_exists') && enum_exists($value::class)) { assert(property_exists($value, 'name')); $value = $value->name; continue; diff --git a/src/ParamConverterInterface.php b/src/ParamConverterInterface.php index 91f95774..cf7479fc 100644 --- a/src/ParamConverterInterface.php +++ b/src/ParamConverterInterface.php @@ -6,8 +6,6 @@ interface ParamConverterInterface { - /** - * @param array $values - */ + /** @param array $values */ public function __invoke(array &$values): void; } diff --git a/src/ParamInjector.php b/src/ParamInjector.php index 2c5ccc6e..bb8a8b9a 100644 --- a/src/ParamInjector.php +++ b/src/ParamInjector.php @@ -13,12 +13,9 @@ final class ParamInjector implements ParamInjectorInterface { - /** @var InjectorInterface */ - private $injector; - - public function __construct(InjectorInterface $injector) - { - $this->injector = $injector; + public function __construct( + private InjectorInterface $injector + ){ } /** diff --git a/src/ParamInjectorInterface.php b/src/ParamInjectorInterface.php index 9accfd65..b2a58623 100644 --- a/src/ParamInjectorInterface.php +++ b/src/ParamInjectorInterface.php @@ -8,8 +8,6 @@ interface ParamInjectorInterface { - /** - * @return array - */ + /** @return array */ public function getArgumentes(MethodInvocation $invocation): array; } diff --git a/src/Queries.php b/src/Queries.php index 97a702c2..d5a793ce 100644 --- a/src/Queries.php +++ b/src/Queries.php @@ -6,23 +6,14 @@ final class Queries { - /** - * @var list - * @readonly - */ - public $classes; - - /** - * @param list $mediaQueryClasses - */ - private function __construct(array $mediaQueryClasses) - { - $this->classes = $mediaQueryClasses; + /** @param list $classes */ + private function __construct( + /** @readonly */ + public array $classes, + ) { } - /** - * @param list $mediaQueryClasses - */ + /** @param list $mediaQueryClasses */ public static function fromClasses(array $mediaQueryClasses): self { return new self($mediaQueryClasses); diff --git a/src/SqlQuery.php b/src/SqlQuery.php index b094388c..2843b040 100644 --- a/src/SqlQuery.php +++ b/src/SqlQuery.php @@ -10,7 +10,7 @@ use PDOStatement; use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerFactoryInterface; use Ray\AuraSqlModule\Pagerfanta\ExtendedPdoAdapter; -use Ray\Di\Di\Named; +use Ray\MediaQuery\Annotation\Qualifier\SqlDir; use Ray\MediaQuery\Exception\InvalidSqlException; use Ray\MediaQuery\Exception\PdoPerformException; @@ -31,46 +31,22 @@ use function strpos; use function trim; +use const JSON_THROW_ON_ERROR; + class SqlQuery implements SqlQueryInterface { - /** @var ExtendedPdoInterface */ - private $pdo; - - /** @var MediaQueryLoggerInterface */ - private $logger; - - /** @var string */ - private $sqlDir; private const C_STYLE_COMMENT = '/\/\*(.*?)\*\//u'; - /** - * @var ?PDOStatement - * @psalm-readonly - */ - private $pdoStatement; - - /** @var AuraSqlPagerFactoryInterface */ - private $pagerFactory; + /** @psalm-readonly */ + private PDOStatement|null $pdoStatement = null; - /** @var ParamConverterInterface */ - private $paramConverter; - - /** - * @Named("sqlDir=Ray\MediaQuery\Annotation\SqlDir") - */ - #[Named('sqlDir=Ray\MediaQuery\Annotation\SqlDir')] public function __construct( - ExtendedPdoInterface $pdo, - string $sqlDir, - MediaQueryLoggerInterface $logger, - AuraSqlPagerFactoryInterface $pagerFactory, - ParamConverterInterface $paramConverter + private ExtendedPdoInterface $pdo, + #[SqlDir] private string $sqlDir, + private MediaQueryLoggerInterface $logger, + private AuraSqlPagerFactoryInterface $pagerFactory, + private ParamConverterInterface $paramConverter, ) { - $this->pdo = $pdo; - $this->logger = $logger; - $this->sqlDir = $sqlDir; - $this->pagerFactory = $pagerFactory; - $this->paramConverter = $paramConverter; } /** @@ -84,7 +60,7 @@ public function exec(string $sqlId, array $values = [], int $fetchMode = PDO::FE /** * {@inheritDoc} */ - public function getRow(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, $fetchArg = '') + public function getRow(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, int|string|callable $fetchArg = ''): array|object|null { $rowList = $this->perform($sqlId, $values, $fetchMode, $fetchArg); if (! count($rowList)) { @@ -119,11 +95,10 @@ public function getCount(string $sqlId, array $values): int /** * @param PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchModode * @param array $values - * @param callable|int|string $fetchArg * * @return array */ - private function perform(string $sqlId, array $values, int $fetchModode, $fetchArg = ''): array + private function perform(string $sqlId, array $values, int $fetchModode, callable|int|string $fetchArg = ''): array { $sqlFile = sprintf('%s/%s.sql', $this->sqlDir, $sqlId); $sqls = $this->getSqls($sqlFile); @@ -134,7 +109,7 @@ private function perform(string $sqlId, array $values, int $fetchModode, $fetchA try { $this->pdoStatement = $this->pdo->perform($sql, $values); } catch (PDOException $e) { - $msg = sprintf('%s in %s.sql with values %s', $e->getMessage(), $sqlId, json_encode($values)); + $msg = sprintf('%s in %s.sql with values %s', $e->getMessage(), $sqlId, json_encode($values, JSON_THROW_ON_ERROR)); throw new PdoPerformException($msg); } @@ -152,11 +127,10 @@ private function perform(string $sqlId, array $values, int $fetchModode, $fetchA /** * @param PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchModode - * @param callable|int|string $fetchArg * * @return array */ - private function fetchAll(int $fetchModode, $fetchArg): array + private function fetchAll(int $fetchModode, callable|int|string $fetchArg): array { assert($this->pdoStatement instanceof PDOStatement); if ($fetchModode === PDO::FETCH_ASSOC) { @@ -176,9 +150,7 @@ private function fetchAll(int $fetchModode, $fetchArg): array }); } - /** - * @return array - */ + /** @return array */ private function getSqls(string $sqlFile): array { if (! file_exists($sqlFile)) { @@ -209,7 +181,7 @@ public function getStatement(): PDOStatement /** * {@inheritDoc} */ - public function getPages(string $sqlId, array $values, int $perPage, string $queryTemplate = '/{?page}', ?string $entity = null): PagesInterface + public function getPages(string $sqlId, array $values, int $perPage, string $queryTemplate = '/{?page}', string|null $entity = null): PagesInterface { ($this->paramConverter)($values); diff --git a/src/SqlQueryFactory.php b/src/SqlQueryFactory.php index dfbd8605..26d70b3f 100644 --- a/src/SqlQueryFactory.php +++ b/src/SqlQueryFactory.php @@ -12,24 +12,22 @@ class SqlQueryFactory { - /** - * @param array $options - */ + /** @param array $options */ public static function getInstance( string $sqlDir, string $dsn, string $username = '', string $password = '', array $options = [], - ?MediaQueryLoggerInterface $logger = null, - ?AuraSqlPagerFactoryInterface $pagerFactory = null + MediaQueryLoggerInterface|null $logger = null, + AuraSqlPagerFactoryInterface|null $pagerFactory = null, ): SqlQueryInterface { return new SqlQuery( new ExtendedPdo($dsn, $username, $password, $options), $sqlDir, $logger ?? new MediaQueryLogger(), $pagerFactory ?? new AuraSqlPagerFactory(new AuraSqlPager(new DefaultView(), [])), - new ParamConverter() + new ParamConverter(), ); } } diff --git a/src/SqlQueryInterface.php b/src/SqlQueryInterface.php index 403ae5e2..a9cd5b91 100644 --- a/src/SqlQueryInterface.php +++ b/src/SqlQueryInterface.php @@ -14,16 +14,15 @@ interface SqlQueryInterface * @param PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchMode * @param int|string|callable $fetchArg */ - public function exec(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, $fetchArg = ''): void; + public function exec(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, int|string|callable $fetchArg = ''): void; /** * @param array $values * @param PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchMode - * @param int|string|callable $fetchArg * * @return array|object|null */ - public function getRow(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, $fetchArg = ''); + public function getRow(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, int|string|callable $fetchArg = ''): array|object|null; /** * @param array $values @@ -32,18 +31,16 @@ public function getRow(string $sqlId, array $values = [], int $fetchMode = PDO:: * * @return array> */ - public function getRowList(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, $fetchArg = ''): array; + public function getRowList(string $sqlId, array $values = [], int $fetchMode = PDO::FETCH_ASSOC, int|string|callable $fetchArg = ''): array; - /** - * @param array $values - */ + /** @param array $values */ public function getCount(string $sqlId, array $values): int; - public function getStatement(): ?PDOStatement; + public function getStatement(): PDOStatement|null; /** * @param array $values * @param ?class-string $entity */ - public function getPages(string $sqlId, array $values, int $perPage, string $queryTemplate = '/{?page}', ?string $entity = null): PagesInterface; + public function getPages(string $sqlId, array $values, int $perPage, string $queryTemplate = '/{?page}', string|null $entity = null): PagesInterface; } diff --git a/src/ToScalarInterface.php b/src/ToScalarInterface.php index 9daca535..b503495a 100644 --- a/src/ToScalarInterface.php +++ b/src/ToScalarInterface.php @@ -6,8 +6,5 @@ interface ToScalarInterface { - /** - * @return scalar - */ - public function toScalar(); + public function toScalar(): bool|string|int|float; } diff --git a/src/WebApiQuery.php b/src/WebApiQuery.php index d03752f4..484cf7de 100644 --- a/src/WebApiQuery.php +++ b/src/WebApiQuery.php @@ -12,28 +12,16 @@ use function json_decode; use function uri_template; +use const JSON_THROW_ON_ERROR; + final class WebApiQuery implements WebApiQueryInterface { - /** @var ClientInterface */ - private $client; - - /** @var MediaQueryLoggerInterface */ - private $logger; - - /** @var array */ - private $uriTemplateBindings; - - /** - * @param array $uriTemplateBindings - * - * @UriTemplateBindings("uriTemplateBindings") - */ - #[UriTemplateBindings('uriTemplateBindings')] - public function __construct(ClientInterface $client, MediaQueryLoggerInterface $logger, array $uriTemplateBindings) - { - $this->client = $client; - $this->logger = $logger; - $this->uriTemplateBindings = $uriTemplateBindings; + /** @param array $uriTemplateBindings */ + public function __construct( + private ClientInterface $client, + private MediaQueryLoggerInterface $logger, + #[UriTemplateBindings] private array $uriTemplateBindings, + ) { } /** @@ -47,7 +35,7 @@ public function request(string $method, string $uri, array $query): array $response = $this->client->request($method, $boundUri, $query); $json = $response->getBody()->getContents(); /** @var array $body */ - $body = json_decode($json, true); + $body = json_decode($json, true, 512, JSON_THROW_ON_ERROR); $this->logger->log($boundUri, $query); return $body; diff --git a/src/WebQueryConfig.php b/src/WebQueryConfig.php index 3ef0f1ae..27f26aa4 100644 --- a/src/WebQueryConfig.php +++ b/src/WebQueryConfig.php @@ -10,22 +10,20 @@ use function json_decode; use function property_exists; +use const JSON_THROW_ON_ERROR; + final class WebQueryConfig { /** @var array */ public $apis = []; - /** @var array */ - public $urlTemplateBindings; - - /** - * @param array $urlTemplateBindings - */ - public function __construct(string $mediaQueryJson, array $urlTemplateBindings = []) - { - $this->urlTemplateBindings = $urlTemplateBindings; + /** @param array $urlTemplateBindings */ + public function __construct( + string $mediaQueryJson, + public array $urlTemplateBindings = [], + ) { /** @var object $json */ - $json = json_decode((string) file_get_contents($mediaQueryJson)); + $json = json_decode((string) file_get_contents($mediaQueryJson), null, 512, JSON_THROW_ON_ERROR); assert(property_exists($json, 'webQuery')); /** @var object $item */ foreach ($json->webQuery as $item) { diff --git a/src/WebQueryInterceptor.php b/src/WebQueryInterceptor.php index 11ee32d5..e2f44b1e 100644 --- a/src/WebQueryInterceptor.php +++ b/src/WebQueryInterceptor.php @@ -11,32 +11,16 @@ class WebQueryInterceptor implements MethodInterceptor { - /** @var WebApiQueryInterface */ - private $webApiQuery; - - /** @var ParamInjectorInterface */ - private $paramInjector; - - /** @var array */ - private $webApiList; - - /** - * @param array $webApiList - * - * @WebApiList("webApiList") - */ - #[WebApiList('webApiList')] - public function __construct(WebApiQueryInterface $webApiQuery, ParamInjectorInterface $paramInjector, array $webApiList) - { - $this->webApiQuery = $webApiQuery; - $this->paramInjector = $paramInjector; - $this->webApiList = $webApiList; + /** @param array $webApiList */ + public function __construct( + private WebApiQueryInterface $webApiQuery, + private ParamInjectorInterface $paramInjector, + #[WebApiList] private array $webApiList, + ) { } - /** - * @return Pages|array - */ - public function invoke(MethodInvocation $invocation) + /** @return Pages|array */ + public function invoke(MethodInvocation $invocation): Pages|array { $method = $invocation->getMethod(); /** @var WebQuery $webQuery */ diff --git a/tests/Fake/Entity/TodoConstruct.php b/tests/Fake/Entity/TodoConstruct.php index 3a3e6186..68bc6294 100644 --- a/tests/Fake/Entity/TodoConstruct.php +++ b/tests/Fake/Entity/TodoConstruct.php @@ -6,15 +6,8 @@ class TodoConstruct { - /** @var string */ - public $id; - - /** @var string */ - public $title; - - public function __construct(string $id, string $title) - { - $this->id = $id; - $this->title = $title; + public function __construct( + public string $id, public string $title + ){ } -} \ No newline at end of file +} diff --git a/tests/Fake/Explicit/TodoAdd.php b/tests/Fake/Explicit/TodoAdd.php index ab4aeb8f..2239b6ac 100644 --- a/tests/Fake/Explicit/TodoAdd.php +++ b/tests/Fake/Explicit/TodoAdd.php @@ -9,12 +9,9 @@ class TodoAdd implements TodoAddInterface { - /** @var SqlQueryInterface */ - private $sqlQuery; - - public function __construct(SqlQueryInterface $sqlQuery) - { - $this->sqlQuery = $sqlQuery; + public function __construct( + private SqlQueryInterface $sqlQuery + ){ } public function __invoke(string $id, string $title): void diff --git a/tests/Fake/Explicit/TodoItem.php b/tests/Fake/Explicit/TodoItem.php index ecb7c30a..f90b4aa9 100644 --- a/tests/Fake/Explicit/TodoItem.php +++ b/tests/Fake/Explicit/TodoItem.php @@ -9,12 +9,9 @@ class TodoItem implements TodoItemInterface { - /** @var SqlQueryInterface */ - private $sqlQuery; - - public function __construct(SqlQueryInterface $sqlQuery) - { - $this->sqlQuery = $sqlQuery; + public function __construct( + private SqlQueryInterface $sqlQuery + ){ } public function __invoke(string $id): array diff --git a/tests/Fake/Explicit/TodoList.php b/tests/Fake/Explicit/TodoList.php index f68c5dfa..fde96f73 100644 --- a/tests/Fake/Explicit/TodoList.php +++ b/tests/Fake/Explicit/TodoList.php @@ -10,12 +10,9 @@ class TodoList implements TodoListInterface { - /** @var SqlQueryInterface */ - private $sqlQuery; - - public function __construct(SqlQueryInterface $sqlQuery) - { - $this->sqlQuery = $sqlQuery; + public function __construct( + private SqlQueryInterface $sqlQuery + ){ } public function __invoke(): Pages diff --git a/tests/Fake/FakeFoo.php b/tests/Fake/FakeFoo.php index 2edeac22..0dfb5daf 100644 --- a/tests/Fake/FakeFoo.php +++ b/tests/Fake/FakeFoo.php @@ -7,16 +7,10 @@ class FakeFoo { - /** @var PromiseAddInterface */ - private $promiseAdd; - - /** @var PromiseItemInterface */ - private $promiseItem; - - public function __construct(PromiseAddInterface $promiseAdd, PromiseItemInterface $promiseItem) - { - $this->promiseAdd = $promiseAdd; - $this->promiseItem = $promiseItem; + public function __construct( + private PromiseAddInterface $promiseAdd, + private PromiseItemInterface $promiseItem + ){ } public function add(): void diff --git a/tests/Fake/FakeString.php b/tests/Fake/FakeString.php index bfbd3d55..3a7cdf2c 100644 --- a/tests/Fake/FakeString.php +++ b/tests/Fake/FakeString.php @@ -2,7 +2,7 @@ namespace Ray\MediaQuery; -class FakeString +class FakeString implements \Stringable { public function __toString(): string { diff --git a/tests/Fake/Queries/DynamicPerPageInterface.php b/tests/Fake/Queries/DynamicPerPageInterface.php index 847f2ace..1c38c414 100644 --- a/tests/Fake/Queries/DynamicPerPageInterface.php +++ b/tests/Fake/Queries/DynamicPerPageInterface.php @@ -12,31 +12,15 @@ interface DynamicPerPageInterface { - /** - * @DbQuery("todo_list") - * @Pager(perPage="perPage", template="/{?page}") - */ #[DbQuery('todo_list'), Pager(perPage: 'perPage', template: '/{?page}')] public function get(int $perPage): Pages; - /** - * @DbQuery("todo_list_scalar_param") - * @Pager(perPage="perPage", template="/{?page}") - */ #[DbQuery('todo_list_scalar_param'), Pager(perPage: 'perPage', template: '/{?page}')] public function getWithScalarParam(int $perPage, int $scalar = 1): Pages; - /** - * @DbQuery("todo_list_fake_string_param") - * @Pager(perPage="perPage", template="/{?page}") - */ #[DbQuery('todo_list_fake_string_param'), Pager(perPage: 'perPage', template: '/{?page}')] public function getWithFakeStringParam(int $perPage, ?FakeString $fakeString = null): Pages; - /** - * @DbQuery("todo_list_fake_bool_param") - * @Pager(perPage="perPage", template="/{?page}") - */ #[DbQuery('todo_list_fake_bool_param'), Pager(perPage: 'perPage', template: '/{?page}')] public function getWithFakeBoolParam(int $perPage, ?FakeBool $fakeBool = null): Pages; } diff --git a/tests/Fake/Queries/DynamicPerPageInvalidInterface.php b/tests/Fake/Queries/DynamicPerPageInvalidInterface.php index de8d664a..0b29b650 100644 --- a/tests/Fake/Queries/DynamicPerPageInvalidInterface.php +++ b/tests/Fake/Queries/DynamicPerPageInvalidInterface.php @@ -10,10 +10,6 @@ interface DynamicPerPageInvalidInterface { - /** - * @DbQuery("todo_list") - * @Pager(perPage="__not_exsits_", template="/{?page}") - */ #[DbQuery('todo_list'), Pager(perPage: '__not_exsits_', template: '/{?page}')] public function __invoke(int $num): Pages; } diff --git a/tests/Fake/Queries/DynamicPerPageInvalidType.php b/tests/Fake/Queries/DynamicPerPageInvalidType.php index ed50834c..5c8656d2 100644 --- a/tests/Fake/Queries/DynamicPerPageInvalidType.php +++ b/tests/Fake/Queries/DynamicPerPageInvalidType.php @@ -10,10 +10,6 @@ interface DynamicPerPageInvalidType { - /** - * @DbQuery("todo_list") - * @Pager(perPage="perPage", template="/{?page}") - */ #[DbQuery('todo_list'), Pager(perPage: 'perPage', template: '/{?page}')] public function __invoke($perPage): Pages; } diff --git a/tests/Fake/Queries/PagerEntityInterface.php b/tests/Fake/Queries/PagerEntityInterface.php index 51919ce2..a13d1cb1 100644 --- a/tests/Fake/Queries/PagerEntityInterface.php +++ b/tests/Fake/Queries/PagerEntityInterface.php @@ -11,10 +11,6 @@ interface PagerEntityInterface { - /** - * @DbQuery(id="todo_list", entity=TodoConstruct::class) - * @Pager(perPage=10, template="/{?page}",) - */ #[DbQuery('todo_list', entity: TodoConstruct::class), Pager(perPage: 10, template: '/{?page}')] public function __invoke(): Pages; } diff --git a/tests/Fake/Queries/PromiseAddInterface.php b/tests/Fake/Queries/PromiseAddInterface.php index c092aa9b..81379242 100644 --- a/tests/Fake/Queries/PromiseAddInterface.php +++ b/tests/Fake/Queries/PromiseAddInterface.php @@ -9,9 +9,6 @@ interface PromiseAddInterface { - /** - * @DbQuery("promise_add") - */ #[DbQuery('promise_add')] public function add(string $id, string $title, DateTimeInterface $time = null): void; } diff --git a/tests/Fake/Queries/PromiseItemInterface.php b/tests/Fake/Queries/PromiseItemInterface.php index ceca4a28..d8039fdf 100644 --- a/tests/Fake/Queries/PromiseItemInterface.php +++ b/tests/Fake/Queries/PromiseItemInterface.php @@ -8,10 +8,6 @@ interface PromiseItemInterface { - /** - * @DbQuery("promise_item", type="row") - * @return array{id: string, title: string, time: string} - */ #[DbQuery('promise_item', type:'row')] public function get(string $id): array; } diff --git a/tests/Fake/Queries/PromiseListInterface.php b/tests/Fake/Queries/PromiseListInterface.php index 852cc075..82c50056 100644 --- a/tests/Fake/Queries/PromiseListInterface.php +++ b/tests/Fake/Queries/PromiseListInterface.php @@ -8,10 +8,6 @@ interface PromiseListInterface { - /** - * @DbQuery("promise_list", type="row_list") - * @return array{id: string, title: string, time: string} - */ #[DbQuery('promise_list')] public function get(): array; } diff --git a/tests/Fake/Queries/TodoAddInterface.php b/tests/Fake/Queries/TodoAddInterface.php index bcd236f3..daf6fac3 100644 --- a/tests/Fake/Queries/TodoAddInterface.php +++ b/tests/Fake/Queries/TodoAddInterface.php @@ -9,10 +9,6 @@ interface TodoAddInterface { - /** - * @DbQuery("todo_add") - * @Transactional - */ #[DbQuery('todo_add'), Transactional] public function __invoke(string $id, string $title): void; } diff --git a/tests/Fake/Queries/TodoConstcuctEntityInterface.php b/tests/Fake/Queries/TodoConstcuctEntityInterface.php index ad962501..1f519624 100644 --- a/tests/Fake/Queries/TodoConstcuctEntityInterface.php +++ b/tests/Fake/Queries/TodoConstcuctEntityInterface.php @@ -9,15 +9,9 @@ interface TodoConstcuctEntityInterface { - /** - * @DbQuery(id="todo_item", entity=TodoConstruct::class, type="row") - */ #[DbQuery('todo_item', entity: TodoConstruct::class, type: "row")] public function getItem(string $id): TodoConstruct; - /** - * @DbQuery(id="todo_list", entity=TodoConstruct::class) - */ #[DbQuery('todo_list', entity: TodoConstruct::class)] public function getList(): array; } diff --git a/tests/Fake/Queries/TodoEntityInterface.php b/tests/Fake/Queries/TodoEntityInterface.php index 172691b3..2502730d 100644 --- a/tests/Fake/Queries/TodoEntityInterface.php +++ b/tests/Fake/Queries/TodoEntityInterface.php @@ -9,16 +9,9 @@ interface TodoEntityInterface { - /** - * @DbQuery(id="todo_item", entity=Todo::class, type="row") - */ #[DbQuery('todo_item', entity: Todo::class, type:'row')] public function getItem(string $id): Todo; - /** - * @DbQuery(id="todo_list",entity=Todo::class) - * @return list - */ #[DbQuery('todo_list', entity: Todo::class)] public function getList(): array; } diff --git a/tests/Fake/Queries/TodoItemInterface.php b/tests/Fake/Queries/TodoItemInterface.php index bede02bd..cc77e8b6 100644 --- a/tests/Fake/Queries/TodoItemInterface.php +++ b/tests/Fake/Queries/TodoItemInterface.php @@ -8,10 +8,6 @@ interface TodoItemInterface { - /** - * @DbQuery("todo_item", type="row") - * @return array{id: string, title: string} - */ #[DbQuery('todo_item', type:'row')] public function __invoke(string $id): array; } diff --git a/tests/Fake/Queries/TodoListInterface.php b/tests/Fake/Queries/TodoListInterface.php index 3f987a7f..7b0beb6c 100644 --- a/tests/Fake/Queries/TodoListInterface.php +++ b/tests/Fake/Queries/TodoListInterface.php @@ -10,10 +10,6 @@ interface TodoListInterface { - /** - * @DbQuery("todo_list") - * @Pager(perPage=10, template="/{?page}") - */ #[DbQuery('todo_list'), Pager(perPage: 10, template: '/{?page}')] public function __invoke(): Pages; } diff --git a/tests/Fake/WebApi/FooItemInterface.php b/tests/Fake/WebApi/FooItemInterface.php index a8ad6a2b..25240f77 100644 --- a/tests/Fake/WebApi/FooItemInterface.php +++ b/tests/Fake/WebApi/FooItemInterface.php @@ -8,9 +8,6 @@ interface FooItemInterface { - /** - * @WebQuery("foo_item") - */ #[WebQuery('foo_item')] public function __invoke(string $id): array; } diff --git a/tests/FakeParamInjectMethod.php b/tests/FakeParamInjectMethod.php index 71fca887..7f682524 100644 --- a/tests/FakeParamInjectMethod.php +++ b/tests/FakeParamInjectMethod.php @@ -12,11 +12,11 @@ public function noInject(int $a): void { } - public function paramInject(?DateTimeInterface $dateTime = null): void + public function paramInject(DateTimeInterface|null $dateTime = null): void { } - public function paramConvert(?FakeBool $bool = null, ?FakeString $string = null, ?DateTimeInterface $dateTime = null): void + public function paramConvert(FakeBool|null $bool = null, FakeString|null $string = null, DateTimeInterface|null $dateTime = null): void { } diff --git a/tests/SqlQueryTest.php b/tests/SqlQueryTest.php index 730f433d..b545b327 100644 --- a/tests/SqlQueryTest.php +++ b/tests/SqlQueryTest.php @@ -42,7 +42,7 @@ protected function setUp(): void __DIR__ . '/sql', $this->log, new AuraSqlPagerFactory(new AuraSqlPager(new DefaultView(), [])), - new ParamConverter() + new ParamConverter(), ); } @@ -57,18 +57,14 @@ public function testExec(): void $this->assertStringContainsString('query: todo_add({"id":"1","title":"run"})', (string) $this->log); } - /** - * @depends testExec - */ + /** @depends testExec */ public function testGetRow(): void { $result = $this->sqlQuery->getRow('todo_item', ['id' => '1']); $this->assertSame($this->insertData, $result); } - /** - * @depends testExec - */ + /** @depends testExec */ public function testGetRowNotFound(): void { $result = $this->sqlQuery->getRow('todo_item', ['id' => '__invalid__']); @@ -76,9 +72,7 @@ public function testGetRowNotFound(): void $this->assertNull($result); } - /** - * @depends testExec - */ + /** @depends testExec */ public function testGetRowList(): void { $result = $this->sqlQuery->getRowList('todo_list', []); @@ -101,9 +95,7 @@ public function testPager(): PagesInterface return $pages; } - /** - * @depends testPager - */ + /** @depends testPager */ public function testPagerCount(Pages $pages): void { $this->assertSame(2, count($pages)); @@ -138,34 +130,26 @@ public function testNotExistsSql(): void $this->sqlQuery->exec('__not_exists', []); } - /** - * @depends testDateTime - */ + /** @depends testDateTime */ public function testGetStatement(SqlQuery $sqlQuery): void { $this->assertInstanceOf(PDOStatement::class, $sqlQuery->getStatement()); } - /** - * @depends testPager - */ + /** @depends testPager */ public function testOffsetExists(Pages $pages): void { $this->assertTrue(isset($pages[1])); } - /** - * @depends testPager - */ + /** @depends testPager */ public function testOffsetSet(Pages $pages): void { $this->expectException(LogicException::class); $pages[1] = ''; } - /** - * @depends testPager - */ + /** @depends testPager */ public function testOffsetUnset(Pages $pages): void { $this->expectException(LogicException::class); From 0a2f6f1232d77da90d215f89a8da6411aa264cd8 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:22:55 +0900 Subject: [PATCH 05/11] Add final prefix --- src/Annotation/DbQuery.php | 5 ----- src/ParamConverter.php | 2 +- src/SqlQuery.php | 2 +- src/SqlQueryFactory.php | 2 +- src/WebQueryInterceptor.php | 2 +- 5 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Annotation/DbQuery.php b/src/Annotation/DbQuery.php index 51363f36..2c8c0bd6 100644 --- a/src/Annotation/DbQuery.php +++ b/src/Annotation/DbQuery.php @@ -6,11 +6,6 @@ use Attribute; -/** - * @Annotation - * @Target("METHOD") - * @NamedArgumentConstructor - */ #[Attribute(Attribute::TARGET_METHOD)] final class DbQuery { diff --git a/src/ParamConverter.php b/src/ParamConverter.php index 568872e6..6eb02a4c 100644 --- a/src/ParamConverter.php +++ b/src/ParamConverter.php @@ -15,7 +15,7 @@ use function print_r; use function property_exists; -class ParamConverter implements ParamConverterInterface +final class ParamConverter implements ParamConverterInterface { private const MYSQL_DATETIME = 'Y-m-d H:i:s'; diff --git a/src/SqlQuery.php b/src/SqlQuery.php index 2843b040..366c2627 100644 --- a/src/SqlQuery.php +++ b/src/SqlQuery.php @@ -33,7 +33,7 @@ use const JSON_THROW_ON_ERROR; -class SqlQuery implements SqlQueryInterface +final class SqlQuery implements SqlQueryInterface { private const C_STYLE_COMMENT = '/\/\*(.*?)\*\//u'; diff --git a/src/SqlQueryFactory.php b/src/SqlQueryFactory.php index 26d70b3f..7e3398cc 100644 --- a/src/SqlQueryFactory.php +++ b/src/SqlQueryFactory.php @@ -10,7 +10,7 @@ use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerFactory; use Ray\AuraSqlModule\Pagerfanta\AuraSqlPagerFactoryInterface; -class SqlQueryFactory +final class SqlQueryFactory { /** @param array $options */ public static function getInstance( diff --git a/src/WebQueryInterceptor.php b/src/WebQueryInterceptor.php index e2f44b1e..cdd98e9c 100644 --- a/src/WebQueryInterceptor.php +++ b/src/WebQueryInterceptor.php @@ -9,7 +9,7 @@ use Ray\MediaQuery\Annotation\Qualifier\WebApiList; use Ray\MediaQuery\Annotation\WebQuery; -class WebQueryInterceptor implements MethodInterceptor +final class WebQueryInterceptor implements MethodInterceptor { /** @param array $webApiList */ public function __construct( From d6f84d1662914183e500d0f70c94a7624c8f003a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:25:39 +0900 Subject: [PATCH 06/11] Remove ext-json --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 308d2103..fbe22d92 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,6 @@ ], "require": { "php": "^8.0", - "ext-json": "*", "ext-pdo": "*", "aura/sql": "^4.0 || ^5.0", "doctrine/annotations": "^1.12", From 4d65ce0875aa14bf2d5316e3ad79977c479855d6 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:38:55 +0900 Subject: [PATCH 07/11] Media access mapping framework --- README.md | 2 ++ composer.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fdfb529d..b581fc7b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Ray.MediaQuery + +## Media access mapping framework [![codecov](https://codecov.io/gh/ray-di/Ray.MediaQuery/branch/1.x/graph/badge.svg?token=QBOPCUPJQV)](https://codecov.io/gh/ray-di/Ray.MediaQuery) [![Type Coverage](https://shepherd.dev/github/ray-di/Ray.MediaQuery/coverage.svg)](https://shepherd.dev/github/ray-di/Ray.MediaQuery) ![Continuous Integration](https://github.com/ray-di/Ray.MediaQuery/workflows/Continuous%20Integration/badge.svg) diff --git a/composer.json b/composer.json index fbe22d92..48a50da3 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "ray/media-query", - "description": "database", + "description": "Media access mapping Framework", "license": "MIT", "authors": [ { From 8da3cc214472b5e5aeb0365e76e741937e947ebe Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:41:18 +0900 Subject: [PATCH 08/11] Add JSON to whitelist ext-json is default in PHP8 --- composer-require-checker.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer-require-checker.json b/composer-require-checker.json index 80da5fd7..f27f31df 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -4,7 +4,8 @@ "static", "self", "parent", "array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "mixed", "never", "Roave\\BetterReflection\\Reflector\\DefaultReflector", - "Roave\\BetterReflection\\Reflector\\ClassReflector" + "Roave\\BetterReflection\\Reflector\\ClassReflector", + "json_decode", "json_encode", "JSON_THROW_ON_ERROR" ], "php-core-extensions" : [ "Core", From 6d888feef33ef2c5fbbb49caea14c4fab9ef9299 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sun, 20 Nov 2022 20:45:09 +0900 Subject: [PATCH 09/11] Exclude PHP 7 code --- src/ClassesInDirectories.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/ClassesInDirectories.php b/src/ClassesInDirectories.php index 575fcaeb..c814c686 100644 --- a/src/ClassesInDirectories.php +++ b/src/ClassesInDirectories.php @@ -6,7 +6,6 @@ use Generator; use Roave\BetterReflection\BetterReflection; -use Roave\BetterReflection\Reflector\ClassReflector; use Roave\BetterReflection\Reflector\DefaultReflector; use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator; @@ -16,8 +15,6 @@ use function class_exists; use function interface_exists; -use const PHP_VERSION_ID; - final class ClassesInDirectories { /** @@ -48,20 +45,11 @@ public static function list(string ...$directories): iterable new AutoloadSourceLocator((new BetterReflection())->astLocator()), ]); - if (PHP_VERSION_ID >= 80000) { - foreach ((new DefaultReflector($sourceLocator))->reflectAllClasses() as $class) { - $className = $class->getName(); - assert(class_exists($className) || interface_exists($className)); - - yield $className; - } - - return; - } + foreach ((new DefaultReflector($sourceLocator))->reflectAllClasses() as $class) { + $className = $class->getName(); + assert(class_exists($className) || interface_exists($className)); - /** @psalm-suppress all */ - foreach ((new ClassReflector($sourceLocator))->getAllClasses() as $class) { // @phpstan-ignore-line - yield $class->getName(); + yield $className; } } } From 0e389f02934588f79300293d6edb51e8a15e4d84 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 21 Nov 2022 12:04:23 +0900 Subject: [PATCH 10/11] Soothe rector --- composer.json | 1 + rector.php | 25 +++++++++++++++++++++++++ src/DbQueryInterceptor.php | 1 - 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 48a50da3..1e1a6ebf 100644 --- a/composer.json +++ b/composer.json @@ -43,6 +43,7 @@ "psr-4": { "Ray\\MediaQuery\\": [ "tests/", + "tests-php81/", "tests/Fake" ] } diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..478d4957 --- /dev/null +++ b/rector.php @@ -0,0 +1,25 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests/*/*Test.php', + __DIR__ . '/tests-php81*/*Test.php', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + $rectorConfig->rule(AnnotationBindingRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80 + ]); +}; diff --git a/src/DbQueryInterceptor.php b/src/DbQueryInterceptor.php index d434dc78..72a08cde 100644 --- a/src/DbQueryInterceptor.php +++ b/src/DbQueryInterceptor.php @@ -61,7 +61,6 @@ private function getFetchMode(DbQuery $dbQuery): int /** * @param array $values * @param PDO::FETCH_ASSOC|PDO::FETCH_CLASS|PDO::FETCH_FUNC $fetchStyle - * @param int|string|callable $fetchArg * * @return array|object|null */ From c88e614807fe29973926a854d7c49a690acbae1e Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Mon, 21 Nov 2022 21:26:29 +0900 Subject: [PATCH 11/11] Revert annotations --- src/Annotation/DbQuery.php | 6 ++++++ src/Annotation/Pager.php | 6 ++++++ src/Annotation/WebQuery.php | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/Annotation/DbQuery.php b/src/Annotation/DbQuery.php index 2c8c0bd6..aaaa7ea8 100644 --- a/src/Annotation/DbQuery.php +++ b/src/Annotation/DbQuery.php @@ -5,7 +5,13 @@ namespace Ray\MediaQuery\Annotation; use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; +/** + * @Annotation + * @Target("METHOD") + * @NamedArgumentConstructor + */ #[Attribute(Attribute::TARGET_METHOD)] final class DbQuery { diff --git a/src/Annotation/Pager.php b/src/Annotation/Pager.php index b52aa0b5..0bc9a0ef 100644 --- a/src/Annotation/Pager.php +++ b/src/Annotation/Pager.php @@ -5,7 +5,13 @@ namespace Ray\MediaQuery\Annotation; use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; +/** + * @Annotation + * @Target("METHOD") + * @NamedArgumentConstructor + */ #[Attribute(Attribute::TARGET_METHOD)] final class Pager { diff --git a/src/Annotation/WebQuery.php b/src/Annotation/WebQuery.php index 8ba2c6bb..5276776a 100644 --- a/src/Annotation/WebQuery.php +++ b/src/Annotation/WebQuery.php @@ -5,7 +5,13 @@ namespace Ray\MediaQuery\Annotation; use Attribute; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; +/** + * @Annotation + * @Target("METHOD") + * @NamedArgumentConstructor + */ #[Attribute(Attribute::TARGET_METHOD)] final class WebQuery {