From a0e130646e184323262920379c9a091f8b2af897 Mon Sep 17 00:00:00 2001 From: Alexandre Gomes Gaigalas Date: Sat, 14 Mar 2026 21:59:17 -0300 Subject: [PATCH] Move to src/ layout, add strict_types, add PHPStan to CI - Move source from library/Respect/Relational/ to src/ and tests from tests/library/Respect/Relational/ to tests/. Update PSR-4 autoload to Respect\Relational\ -> src/ with autoload-dev for tests. - Add declare(strict_types=1) to all 12 PHP files. This surfaced two bugs: Mapper::getAllProperties() passing false to preg_match when getDocComment() returns false, and Sql::normalizeParts() passing a Sql object to stripos() instead of a string. Both fixed. - Raise PHPStan to level 2 with ignores for magic methods, properties, and Style test namespace resolution. - Modernize CI: split into tests and static-analysis jobs, update to actions/checkout@v6 and ramsey/composer-install@v3. --- .github/workflows/ci.yml | 38 ++++++++----------- composer.json | 7 +++- phpcs.xml.dist | 2 +- phpstan.neon.dist | 10 +++-- phpunit.xml.dist | 2 +- {library/Respect/Relational => src}/Db.php | 2 + .../Respect/Relational => src}/Mapper.php | 9 ++++- {library/Respect/Relational => src}/Sql.php | 4 +- .../Respect/Relational => }/DbTest.php | 2 + .../Respect/Relational => }/MapperTest.php | 2 + .../Respect/Relational => }/SqlTest.php | 2 + .../Styles/AbstractStyleTest.php | 2 + .../Relational => }/Styles/CakePHPTest.php | 2 + .../Relational => }/Styles/NorthWindTest.php | 2 + .../Relational => }/Styles/PluralTest.php | 2 + .../Relational => }/Styles/SakilaTest.php | 2 + .../Relational => }/Styles/StandardTest.php | 2 + 17 files changed, 62 insertions(+), 30 deletions(-) rename {library/Respect/Relational => src}/Db.php (99%) rename {library/Respect/Relational => src}/Mapper.php (98%) rename {library/Respect/Relational => src}/Sql.php (98%) rename tests/{library/Respect/Relational => }/DbTest.php (99%) rename tests/{library/Respect/Relational => }/MapperTest.php (99%) rename tests/{library/Respect/Relational => }/SqlTest.php (99%) rename tests/{library/Respect/Relational => }/Styles/AbstractStyleTest.php (98%) rename tests/{library/Respect/Relational => }/Styles/CakePHPTest.php (99%) rename tests/{library/Respect/Relational => }/Styles/NorthWindTest.php (99%) rename tests/{library/Respect/Relational => }/Styles/PluralTest.php (99%) rename tests/{library/Respect/Relational => }/Styles/SakilaTest.php (99%) rename tests/{library/Respect/Relational => }/Styles/StandardTest.php (99%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 757d47a..c02b873 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,30 +6,24 @@ on: pull_request: jobs: - test: + tests: + name: Tests runs-on: ubuntu-latest - strategy: - matrix: - php: [ '8.5' ] steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 + - uses: actions/checkout@v6 + - uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} + php-version: '8.5' + - uses: ramsey/composer-install@v3 + - run: composer phpunit - - name: Get composer cache - uses: actions/cache@v4 + static-analysis: + name: Static Analysis + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + - uses: shivammathur/setup-php@v2 with: - path: ~/.cache/composer - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --no-progress --prefer-dist --no-interaction - - - name: Run PHPUnit - run: vendor/bin/phpunit --configuration phpunit.xml.dist \ No newline at end of file + php-version: '8.5' + - uses: ramsey/composer-install@v3 + - run: composer phpstan diff --git a/composer.json b/composer.json index 6e131c8..22f2251 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,12 @@ }, "autoload": { "psr-4": { - "Respect\\": "library/Respect" + "Respect\\Relational\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Respect\\Relational\\": "tests/" } }, "scripts": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 0572243..1219d48 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -11,7 +11,7 @@ - library/ + src/ tests/ diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6250649..94b4829 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,11 +1,15 @@ parameters: - level: 1 + level: 2 paths: - - library/ + - src/ - tests/ ignoreErrors: - - message: '/Call to an undefined static method Respect\\Relational\\Sql::\w+\(\)\./' + - message: '/Call to an undefined (static )?method Respect\\Relational\\(Sql|Db|Mapper)::\w+\(\)\./' - message: '/Call to an undefined static method Respect\\Data\\Collections\\(Collection|Filtered|Mix|Typed)::\w+\(\)\./' + - message: '/Access to an undefined property Respect\\Relational\\Mapper::\$\w+\./' - message: '/Unsafe usage of new static\(\)\./' - message: '/Cannot unset property .+ because it might have hooks in a subclass\./' - message: '/Array has \d+ duplicate keys/' + - + message: '/unknown class/' + path: tests/Styles/ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7c4d0df..da3a218 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,7 +11,7 @@ - library/ + src/ diff --git a/library/Respect/Relational/Db.php b/src/Db.php similarity index 99% rename from library/Respect/Relational/Db.php rename to src/Db.php index 54402dd..ee53fc2 100644 --- a/library/Respect/Relational/Db.php +++ b/src/Db.php @@ -1,5 +1,7 @@ getStyle(); return $entity === $s->composed($parent, $next) @@ -647,7 +653,8 @@ protected function getAllProperties($object) $cols = get_object_vars($object); $ref = new \ReflectionClass($object); foreach ($ref->getProperties() as $prop) { - if (preg_match('/@Relational\\\isNotColumn/', $prop->getDocComment())) { + $docComment = $prop->getDocComment(); + if ($docComment !== false && preg_match('/@Relational\\\isNotColumn/', $docComment)) { continue; } $cols[$prop->name] = $prop->getValue($object); diff --git a/library/Respect/Relational/Sql.php b/src/Sql.php similarity index 98% rename from library/Respect/Relational/Sql.php rename to src/Sql.php index 2b9d3a4..b933fc1 100644 --- a/library/Respect/Relational/Sql.php +++ b/src/Sql.php @@ -1,5 +1,7 @@ getParams()); - if (0 !== stripos($value, '(')) { + if (0 !== stripos((string) $value, '(')) { $value = Sql::enclose($value); } $newParts[$key] = $value; diff --git a/tests/library/Respect/Relational/DbTest.php b/tests/DbTest.php similarity index 99% rename from tests/library/Respect/Relational/DbTest.php rename to tests/DbTest.php index d374c9c..aa7e85d 100644 --- a/tests/library/Respect/Relational/DbTest.php +++ b/tests/DbTest.php @@ -1,5 +1,7 @@