From bb96d5dc3534865b6cbe5bac52e57075d9586b95 Mon Sep 17 00:00:00 2001 From: Craig Duncan Date: Sat, 19 Jan 2019 17:41:44 +0000 Subject: [PATCH] Use phpstan to statically analyse the code --- .travis.yml | 5 +++++ composer.json | 3 +++ phpstan-tests.neon | 7 +++++++ src/Blade.php | 5 +++-- src/BladeInstance.php | 11 +++++------ src/ConditionHandler.php | 2 +- src/Directives.php | 4 ++++ tests/BladeMockTest.php | 12 ++++++++++-- 8 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 phpstan-tests.neon diff --git a/.travis.yml b/.travis.yml index be74667..5403576 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,11 @@ jobs: script: - vendor/bin/phpcs --standard=phpcs.xml + - stage: code quality + script: + - vendor/bin/phpstan analyse --level=max src + - vendor/bin/phpstan analyse --level=max --configuration=phpstan-tests.neon tests + - stage: coverage script: - vendor/bin/phpunit --coverage-clover=coverage.xml diff --git a/composer.json b/composer.json index 4fe1967..49fc49a 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,9 @@ "duncan3dc/object-intruder": "^0.3.0", "maglnet/composer-require-checker": "^1.1", "mockery/mockery": "^1.2", + "phpstan/phpstan": "^0.10", + "phpstan/phpstan-mockery": "^0.10", + "phpstan/phpstan-phpunit": "^0.10", "squizlabs/php_codesniffer": "^3.4", "phpunit/phpunit": "^7.3" }, diff --git a/phpstan-tests.neon b/phpstan-tests.neon new file mode 100644 index 0000000..0922aa7 --- /dev/null +++ b/phpstan-tests.neon @@ -0,0 +1,7 @@ +parameters: + ignoreErrors: + - '/Access to an undefined property duncan3dc\\ObjectIntruder\\Intruder::/' + +includes: + - vendor/phpstan/phpstan-mockery/extension.neon + - vendor/phpstan/phpstan-phpunit/extension.neon diff --git a/src/Blade.php b/src/Blade.php index 8c28b7a..b39832b 100644 --- a/src/Blade.php +++ b/src/Blade.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\View\View as ViewInterface; use function is_dir; +use function is_string; use function realpath; /** @@ -12,7 +13,7 @@ class Blade { /** - * @var BladeInstance $instance The internal cache of the BladeInstance to only instantiate it once + * @var BladeInstance|null $instance The internal cache of the BladeInstance to only instantiate it once */ private static $instance; @@ -40,7 +41,7 @@ public static function getInstance(): BladeInterface if (!static::$instance) { # Calculate the parent of the vendor directory $path = realpath(__DIR__ . "/../../../.."); - if (!is_dir($path)) { + if (!is_string($path) || !is_dir($path)) { throw new \RuntimeException("Unable to locate the root directory: {$path}"); } diff --git a/src/BladeInstance.php b/src/BladeInstance.php index 2dde570..7d1aa02 100644 --- a/src/BladeInstance.php +++ b/src/BladeInstance.php @@ -2,7 +2,6 @@ namespace duncan3dc\Laravel; -use Illuminate\Contracts\View\Factory as FactoryInterface; use Illuminate\Contracts\View\View as ViewInterface; use Illuminate\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; @@ -35,12 +34,12 @@ class BladeInstance implements BladeInterface private $directives; /** - * @var Factory $factory The internal cache of the Factory to only instantiate it once. + * @var Factory|null $factory The internal cache of the Factory to only instantiate it once. */ private $factory; /** - * @var FileViewFinder $finder The internal cache of the FileViewFinder to only instantiate it once. + * @var FileViewFinder|null $finder The internal cache of the FileViewFinder to only instantiate it once. */ private $finder; @@ -50,7 +49,7 @@ class BladeInstance implements BladeInterface private $compiler; /** - * @var ConditionHandler $conditionHandler The custom conditionals that have been registered. + * @var ConditionHandler|null $conditionHandler The custom conditionals that have been registered. */ private $conditionHandler; @@ -91,9 +90,9 @@ private function getViewFinder(): FileViewFinder /** * Get the laravel view factory. * - * @return FactoryInterface + * @return Factory */ - private function getViewFactory(): FactoryInterface + private function getViewFactory(): Factory { if ($this->factory) { return $this->factory; diff --git a/src/ConditionHandler.php b/src/ConditionHandler.php index 07eea2a..a57c830 100644 --- a/src/ConditionHandler.php +++ b/src/ConditionHandler.php @@ -36,7 +36,7 @@ public function add(string $name, callable $handler): ConditionHandler * Call a registered conditional directive. * * @param string $name - * @param mixed $params + * @param mixed ...$params * * @return mixed */ diff --git a/src/Directives.php b/src/Directives.php index baff1c4..49110e0 100644 --- a/src/Directives.php +++ b/src/Directives.php @@ -3,7 +3,9 @@ namespace duncan3dc\Laravel; use Illuminate\View\Compilers\BladeCompiler; +use function assert; use function in_array; +use function is_string; use function strlen; use function substr; use function trim; @@ -162,6 +164,7 @@ public function register(BladeCompiler $blade): void if ($this->css !== null) { $blade->directive("css", function ($parameter) { + assert(is_string($this->css)); $file = $this->assetify($parameter, "css", $this->css); return ""; }); @@ -169,6 +172,7 @@ public function register(BladeCompiler $blade): void if ($this->js !== null) { $blade->directive("js", function ($parameter) { + assert(is_string($this->js)); $file = $this->assetify($parameter, "js", $this->js); return ""; }); diff --git a/tests/BladeMockTest.php b/tests/BladeMockTest.php index 3f9bd40..965eef9 100644 --- a/tests/BladeMockTest.php +++ b/tests/BladeMockTest.php @@ -3,19 +3,27 @@ namespace duncan3dc\LaravelTests; use duncan3dc\Laravel\BladeInstance; +use duncan3dc\Laravel\BladeInterface; use duncan3dc\ObjectIntruder\Intruder; use Illuminate\Contracts\View\View as ViewInterface; -use Illuminate\Contracts\View\Factory as FactoryInterface; +use Illuminate\View\Factory; use Illuminate\View\FileViewFinder; use Mockery; +use Mockery\MockInterface; use PHPUnit\Framework\TestCase; class BladeMockTest extends TestCase { + /** @var BladeInterface */ private $blade; + + /** @var FileViewFinder|MockInterface */ private $finder; + + /** @var Factory|MockInterface */ private $factory; + public function setUp() { $this->blade = new BladeInstance(__DIR__ . "/views", Utils::getCachePath()); @@ -25,7 +33,7 @@ public function setUp() $this->finder = Mockery::mock(FileViewFinder::class); $intruder->finder = $this->finder; - $this->factory = Mockery::mock(FactoryInterface::class); + $this->factory = Mockery::mock(Factory::class); $intruder->factory = $this->factory; }