From 55e4f442f75ac1c5adb3c888d1f83f0b15c03d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Sat, 18 Apr 2020 18:34:28 +0200 Subject: [PATCH] Add phpstan to test the project --- .travis.yml | 2 +- build.xml | 8 ++ captainhook.json | 4 + composer.json | 2 + composer.lock | 103 +++++++++++++++++- phpstan.neon | 18 +++ .../Magento/Autoload/FactoryAutoloader.php | 4 +- .../Reflection/MagicMethodReflection.php | 2 +- .../Cache/FileCacheStorageUnitTest.php | 8 +- .../Autoload/FactoryAutoloaderUnitTest.php | 6 +- .../Autoload/ProxyAutoloaderUnitTest.php | 6 +- ...agerDynamicReturnTypeExtensionUnitTest.php | 2 +- 12 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index dc06988..a1cad01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,9 +19,9 @@ script: - composer validate - ./vendor/bin/phing security:check - ./vendor/bin/phing sniff + - ./vendor/bin/phing static-analysis - if [[ $TEST_COVERAGE == 'true' ]]; then ./vendor/bin/phing unit-with-coverage ; fi - if [[ $TEST_COVERAGE != 'true' ]]; then ./vendor/bin/phing unit ; fi after_script: - if [[ $TEST_COVERAGE == 'true' ]]; then ./vendor/bin/coveralls -v ; fi - diff --git a/build.xml b/build.xml index 8141dc4..e600b8b 100644 --- a/build.xml +++ b/build.xml @@ -39,4 +39,12 @@ + + + + + + diff --git a/captainhook.json b/captainhook.json index 303fb84..2721bc6 100644 --- a/captainhook.json +++ b/captainhook.json @@ -14,6 +14,10 @@ "pre-push": { "enabled": false, "actions": [ + { + "action": "./vendor/bin/phing static-analysis", + "options": [] + }, { "action": "./vendor/bin/phing security:check", "options": [] diff --git a/composer.json b/composer.json index b881965..c889fb2 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,8 @@ "mikey179/vfsstream": "^1.6", "nikic/php-parser": "^4.3", "phing/phing": "^2.16", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan-phpunit": "^0.12.8", "phpunit/phpunit": "^8.5.3", "squizlabs/php_codesniffer": "^3.4" }, diff --git a/composer.lock b/composer.lock index b8771a2..37acfab 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bcd694f6b40f1aa8edfc1d4e25aa0130", + "content-hash": "4474cde4439e2e7ec16a53c0ff778102", "packages": [ { "name": "nette/neon", @@ -968,6 +968,107 @@ ], "time": "2020-03-05T15:02:03+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.0.4", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "2e041def501d661b806f50000c8a4dccbd4907b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/2e041def501d661b806f50000c8a4dccbd4907b4", + "reference": "2e041def501d661b806f50000c8a4dccbd4907b4", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.1 || ^2.0", + "php": "^7.1", + "phpstan/phpstan": ">=0.11.6" + }, + "require-dev": { + "composer/composer": "^1.8", + "consistence/coding-standard": "^3.8", + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4", + "ergebnis/composer-normalize": "^2.0.2", + "jakub-onderka/php-parallel-lint": "^1.0", + "phing/phing": "^2.16", + "phpstan/phpstan-strict-rules": "^0.11", + "slevomat/coding-standard": "^5.0.4" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "time": "2020-03-31T16:00:42+00:00" + }, + { + "name": "phpstan/phpstan-phpunit", + "version": "0.12.8", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-phpunit.git", + "reference": "7232c17e2493dc598173da784477ce0afb2c4e0e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/7232c17e2493dc598173da784477ce0afb2c4e0e", + "reference": "7232c17e2493dc598173da784477ce0afb2c4e0e", + "shasum": "" + }, + "require": { + "php": "~7.1", + "phpstan/phpstan": "^0.12.6" + }, + "conflict": { + "phpunit/phpunit": "<7.0" + }, + "require-dev": { + "consistence/coding-standard": "^3.5", + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4", + "ergebnis/composer-normalize": "^2.0.2", + "jakub-onderka/php-parallel-lint": "^1.0", + "phing/phing": "^2.16.0", + "phpstan/phpstan-strict-rules": "^0.12", + "phpunit/phpunit": "^7.0", + "satooshi/php-coveralls": "^1.0", + "slevomat/coding-standard": "^4.7.2" + }, + "type": "phpstan-extension", + "extra": { + "branch-alias": { + "dev-master": "0.12-dev" + }, + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPUnit extensions and rules for PHPStan", + "time": "2020-04-17T08:04:10+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "7.0.10", diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..aba84cb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,18 @@ +parameters: + level: 7 + inferPrivatePropertyTypeFromConstructor: true + fileExtensions: + - php + paths: + - src/bitExpert + - tests/bitExpert + ignoreErrors: + - + message: '~Call to an undefined method ReflectionType::getName~' + path: src/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloader.php + - + message: '~Parameter #1 \$argument of class ReflectionClass constructor expects~' + path: src/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloader.php + - + message: '~Class Magento\\Framework\\DataObject not found~' + path: src/bitExpert/PHPStan/Magento/Reflection/Framework/DataObjectMagicMethodReflectionExtension.php diff --git a/src/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloader.php b/src/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloader.php index c82b7a2..358b845 100644 --- a/src/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloader.php +++ b/src/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloader.php @@ -49,10 +49,10 @@ public function autoload(string $class): void /** * Generate the factory file content as Magento would. * - * @param $class + * @param string $class * @return string */ - protected function getFileContents($class): string + protected function getFileContents(string $class): string { $namespace = explode('\\', ltrim($class, '\\')); $factoryClassname = array_pop($namespace); diff --git a/src/bitExpert/PHPStan/Magento/Reflection/MagicMethodReflection.php b/src/bitExpert/PHPStan/Magento/Reflection/MagicMethodReflection.php index 255d2f0..4e621ea 100644 --- a/src/bitExpert/PHPStan/Magento/Reflection/MagicMethodReflection.php +++ b/src/bitExpert/PHPStan/Magento/Reflection/MagicMethodReflection.php @@ -28,7 +28,7 @@ class MagicMethodReflection implements MethodReflection */ private $declaringClass; /** - * @var array + * @var ParametersAcceptor[] */ private $variants; diff --git a/tests/bitExpert/PHPStan/Magento/Autoload/Cache/FileCacheStorageUnitTest.php b/tests/bitExpert/PHPStan/Magento/Autoload/Cache/FileCacheStorageUnitTest.php index c1bec85..3059362 100644 --- a/tests/bitExpert/PHPStan/Magento/Autoload/Cache/FileCacheStorageUnitTest.php +++ b/tests/bitExpert/PHPStan/Magento/Autoload/Cache/FileCacheStorageUnitTest.php @@ -37,7 +37,7 @@ public function setUp(): void /** * @test */ - public function nullReturnedWhenLookingUpNonExistentFileInCache() + public function nullReturnedWhenLookingUpNonExistentFileInCache(): void { $absFilename = $this->storage->load('test.txt', ''); @@ -47,7 +47,7 @@ public function nullReturnedWhenLookingUpNonExistentFileInCache() /** * @test */ - public function absoluteFilenameReturnedWhenLookingUpExistentFileInCache() + public function absoluteFilenameReturnedWhenLookingUpExistentFileInCache(): void { vfsStream::create( ['4b' => ['6f' => ['4b6fcb2d521ef0fd442a5301e7932d16cc9f375a.php' => 'Lorem ipsum']]], @@ -62,7 +62,7 @@ public function absoluteFilenameReturnedWhenLookingUpExistentFileInCache() /** * @test */ - public function addingFileToCacheSucceeds() + public function addingFileToCacheSucceeds(): void { $this->storage->save('test.txt', '', 'Lorem ipsum'); $absFilename = $this->storage->load('test.txt', ''); @@ -73,7 +73,7 @@ public function addingFileToCacheSucceeds() /** * @test */ - public function addingFileToCacheFails() + public function addingFileToCacheFails(): void { $this->expectException(InvalidArgumentException::class); diff --git a/tests/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloaderUnitTest.php b/tests/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloaderUnitTest.php index 5e312d7..d0c4463 100644 --- a/tests/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloaderUnitTest.php +++ b/tests/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloaderUnitTest.php @@ -36,7 +36,7 @@ public function setUp(): void /** * @test */ - public function autoloaderIgnoresClassesWithoutFactoryPostfix() + public function autoloaderIgnoresClassesWithoutFactoryPostfix(): void { $this->storage->expects($this->never()) ->method('load'); @@ -47,7 +47,7 @@ public function autoloaderIgnoresClassesWithoutFactoryPostfix() /** * @test */ - public function autoloaderUsesCachedFileWhenFound() + public function autoloaderUsesCachedFileWhenFound(): void { $this->storage->expects($this->once()) ->method('load') @@ -61,7 +61,7 @@ public function autoloaderUsesCachedFileWhenFound() /** * @test */ - public function autoloaderGeneratesCacheFileWhenNotFoundInCache() + public function autoloaderGeneratesCacheFileWhenNotFoundInCache(): void { $this->storage->expects($this->atMost(2)) ->method('load') diff --git a/tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php b/tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php index c3357ca..8d4d0df 100644 --- a/tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php +++ b/tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php @@ -36,7 +36,7 @@ public function setUp(): void /** * @test */ - public function autoloaderIgnoresClassesWithoutProxyPostfix() + public function autoloaderIgnoresClassesWithoutProxyPostfix(): void { $this->storage->expects($this->never()) ->method('load'); @@ -47,7 +47,7 @@ public function autoloaderIgnoresClassesWithoutProxyPostfix() /** * @test */ - public function autoloaderUsesCachedFileWhenFound() + public function autoloaderUsesCachedFileWhenFound(): void { $this->storage->expects($this->once()) ->method('load') @@ -61,7 +61,7 @@ public function autoloaderUsesCachedFileWhenFound() /** * @test */ - public function autoloaderGeneratesCacheFileWhenNotFoundInCache() + public function autoloaderGeneratesCacheFileWhenNotFoundInCache(): void { // little hack: the proxy autoloader will use Reflection to look for a class without the \Proxy prefix, // to avoid having another stub class file, we define an class alias here diff --git a/tests/bitExpert/PHPStan/Magento/Type/ObjectManagerDynamicReturnTypeExtensionUnitTest.php b/tests/bitExpert/PHPStan/Magento/Type/ObjectManagerDynamicReturnTypeExtensionUnitTest.php index 63136cb..8a983f4 100644 --- a/tests/bitExpert/PHPStan/Magento/Type/ObjectManagerDynamicReturnTypeExtensionUnitTest.php +++ b/tests/bitExpert/PHPStan/Magento/Type/ObjectManagerDynamicReturnTypeExtensionUnitTest.php @@ -10,7 +10,7 @@ */ declare(strict_types=1); -namespace PHPStan\Magento\Type; +namespace bitExpert\PHPStan\Magento\Type; use bitExpert\PHPStan\Magento\Type\ObjectManagerDynamicReturnTypeExtension; use PhpParser\Node\Expr\MethodCall;