Skip to content

Commit

Permalink
Use phpstan to statically analyse the code
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Jan 19, 2019
1 parent 0bbb306 commit bb96d5d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
7 changes: 7 additions & 0 deletions phpstan-tests.neon
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\View\View as ViewInterface;
use function is_dir;
use function is_string;
use function realpath;

/**
Expand All @@ -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;

Expand Down Expand Up @@ -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}");
}

Expand Down
11 changes: 5 additions & 6 deletions src/BladeInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/ConditionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,13 +164,15 @@ 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 "<link rel='stylesheet' type='text/css' href='{$file}'>";
});
}

if ($this->js !== null) {
$blade->directive("js", function ($parameter) {
assert(is_string($this->js));
$file = $this->assetify($parameter, "js", $this->js);
return "<script type='text/javascript' src='{$file}'></script>";
});
Expand Down
12 changes: 10 additions & 2 deletions tests/BladeMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}

Expand Down

0 comments on commit bb96d5d

Please sign in to comment.