diff --git a/.gitattributes b/.gitattributes index 13a07c68c..7644e7766 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,6 @@ /tests export-ignore /phpcs.xml.dist export-ignore +/phpstan.xml.dist export-ignore /phpunit.xml.dist export-ignore /.gitattributes export-ignore /.github export-ignore diff --git a/.github/workflows/static-analysis.yaml b/.github/workflows/static-analysis.yaml new file mode 100644 index 000000000..a28af8204 --- /dev/null +++ b/.github/workflows/static-analysis.yaml @@ -0,0 +1,35 @@ +name: "Static Analysis" + +on: + pull_request: + push: + branches: + - "master" + +jobs: + static-analysis-phpstan: + name: "Static Analysis with PHPStan" + runs-on: "ubuntu-20.04" + + strategy: + matrix: + php-version: + - "7.2" + + steps: + - name: "Checkout code" + uses: "actions/checkout@v2" + + - name: "Install PHP" + uses: "shivammathur/setup-php@v2" + with: + coverage: "none" + php-version: "${{ matrix.php-version }}" + tools: "cs2pr" + extensions: pdo_sqlite + + - name: "Install dependencies with Composer" + uses: "ramsey/composer-install@v1" + + - name: "Run a static analysis with phpstan/phpstan" + run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr" diff --git a/.gitignore b/.gitignore index 916d08cca..dc0d257f5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ phpunit.xml composer.lock .idea/ phpcs.xml +phpstan.xml .phpcs-cache /doc/_build/* /.phpunit.result.cache diff --git a/composer.json b/composer.json index 40f77c1e7..346e82822 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "doctrine/phpcr-odm": "^1.3|^2.0", "jackalope/jackalope-doctrine-dbal": "^1.1.5", "ocramius/proxy-manager": "^1.0|^2.0", + "phpstan/phpstan": "^0.12.65", "phpunit/phpunit": "^8.0||^9.0", "psr/container": "^1.0", "symfony/dependency-injection": "^3.0|^4.0|^5.0", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 000000000..037e731dc --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,12 @@ +parameters: + ignoreErrors: + - + message: "#^Class JMS\\\\Serializer\\\\Handler\\\\iterable not found\\.$#" + count: 2 + path: src/Handler/IteratorHandler.php + + - + message: "#^Variable \\$previousEntityLoaderState might not be defined\\.$#" + count: 1 + path: src/XmlDeserializationVisitor.php + diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 000000000..c3c86d508 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,13 @@ +parameters: + level: 1 + + ignoreErrors: + - '~Class Doctrine\\ODM\\MongoDB\\PersistentCollection not found~' + - '~Class Symfony\\Component\\Translation\\TranslatorInterface not found~' + + paths: + - %currentWorkingDirectory%/src + - %currentWorkingDirectory%/tests + +includes: + - phpstan-baseline.neon diff --git a/src/Handler/FormErrorHandler.php b/src/Handler/FormErrorHandler.php index b5101960b..3ef3f3de5 100644 --- a/src/Handler/FormErrorHandler.php +++ b/src/Handler/FormErrorHandler.php @@ -12,6 +12,8 @@ use Symfony\Component\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface as TranslatorContract; +use function get_class; + final class FormErrorHandler implements SubscribingHandlerInterface { /** @@ -53,7 +55,8 @@ public function __construct(?object $translator = null, string $translationDomai 'The first argument passed to %s must be instance of %s or %s, %s given', self::class, TranslatorInterface::class, - TranslatorContract::class + TranslatorContract::class, + get_class($translator) )); } diff --git a/src/XmlSerializationVisitor.php b/src/XmlSerializationVisitor.php index 48439eb67..a5edbece5 100644 --- a/src/XmlSerializationVisitor.php +++ b/src/XmlSerializationVisitor.php @@ -399,10 +399,6 @@ public function getCurrentMetadata(): ?PropertyMetadata public function getDocument(): \DOMDocument { - if (null === $this->document) { - $this->document = $this->createDocument(); - } - return $this->document; } diff --git a/tests/Metadata/Driver/BaseDriverTest.php b/tests/Metadata/Driver/BaseDriverTest.php index 42376c910..efea4d4bf 100644 --- a/tests/Metadata/Driver/BaseDriverTest.php +++ b/tests/Metadata/Driver/BaseDriverTest.php @@ -620,10 +620,6 @@ public function testExcludePropertyNoPublicAccessorException() { $first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor')); - if ($this instanceof PhpDriverTest) { - return; - } - self::assertArrayHasKey('id', $first->propertyMetadata); self::assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata); }