Skip to content

Commit a5fe914

Browse files
authored
v0.6.2 (#13)
* Bump dependency * Refrain from adding newline in SVG's * Update CHANGELOG.md with new version * Add phpstan * Configure phpstan * Fix phpstan errors, remove test for now private properties * Include phpstan in ci * Change exclude pattern so that src/Blade.php isn't excluded * Fix cs error * Update squizlabs/php_codesniffer version * Update CHANGELOG.md * Add test for null responses to increase coverage
1 parent fad4f58 commit a5fe914

10 files changed

+88
-46
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ indent_size = 4
1313

1414
[*.md]
1515
trim_trailing_whitespace = false
16+
17+
[*.svg]
18+
insert_final_newline = false

.github/workflows/ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
php: [7.3, 7.4, 8.0]
11+
php: [7.3, 7.4, 8.0, 8.1]
1212

1313
name: PHP ${{ matrix.php }}
1414

@@ -24,6 +24,8 @@ jobs:
2424
run: composer install --prefer-dist --no-interaction
2525
- name: "Check coding standards"
2626
run: vendor/bin/phpcs
27+
- name: "Analyze code with PHPStan"
28+
run: vendor/bin/phpstan
2729
- name: "Run test suite"
2830
run: vendor/bin/phpunit --coverage-clover=coverage.xml
2931
- name: "Upload coverage to Codecov"

.phpcs.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<file>src</file>
44
<file>tests</file>
55

6-
<exclude-pattern>*blade.php$</exclude-pattern>
6+
<exclude-pattern>tests/views/*</exclude-pattern>
77
<exclude-pattern>*/cache/*$</exclude-pattern>
88

99
<arg name="basepath" value="."/>

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 0.6.2 (released 2022-07-14)
4+
5+
- Added PHPStan
6+
- Updated squizlabs/php_codesniffer to 3.7
7+
- Updated illuminate/view to 8.83
8+
39
## 0.6.1 (released 2021-05-25)
410

511
- Updated illuminate/view to 8.42

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
],
1717
"require": {
1818
"php": "^7.3 || ^8.0",
19-
"illuminate/view": "^8.42",
19+
"illuminate/view": "^8.83",
2020
"soberwp/controller": "^2.1"
2121
},
2222
"require-dev": {
2323
"10up/wp_mock": "^0.4.2",
24+
"phpstan/phpstan": "^1.8",
2425
"phpunit/phpunit": "9.4.4",
25-
"squizlabs/php_codesniffer": "^3.6"
26+
"squizlabs/php_codesniffer": "^3.7"
2627
},
2728
"config": {
2829
"preferred-install": "dist",
@@ -51,8 +52,9 @@
5152
}
5253
},
5354
"scripts": {
55+
"cs": "vendor/bin/phpcs",
5456
"test": "vendor/bin/phpunit",
55-
"cs": "vendor/bin/phpcs"
57+
"stan": "vendor/bin/phpstan"
5658
},
5759
"suggest": {
5860
"wordplate/wordplate": "WordPlate is a modern WordPress stack which simplifies WordPress development (^9.0)."

phpstan.neon

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
level: 3
3+
paths:
4+
- src
5+
- tests
6+
excludePaths:
7+
- tests/cache
8+
ignoreErrors:
9+
- '#Function apply_filters not found\.#'
10+
- '#Function get_current_blog_id not found\.#'
11+
- '#Function is_multisite not found\.#'
12+
- '#Call to an undefined static method Fiskhandlarn\\BladeFacade::[\w]+\(\)\.#'
13+
reportUnmatchedIgnoredErrors: false

src/Blade.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@
3535
*/
3636
class Blade
3737
{
38+
/**
39+
* Path to compiled views.
40+
*
41+
* @var string
42+
*/
43+
private $cachePath;
44+
45+
/**
46+
* The compiler implementation.
47+
*
48+
* @var \Illuminate\View\Compilers\CompilerInterface
49+
*/
50+
private $compiler;
3851

3952
/**
4053
* The container.
@@ -71,11 +84,9 @@ class Blade
7184
* @param string $cachePath
7285
* @param bool $createCacheDirectory
7386
*/
74-
public function __construct($viewPaths, $cachePath, $createCacheDirectory = true)
87+
public function __construct($viewPaths, string $cachePath, bool $createCacheDirectory = true)
7588
{
76-
$this->viewPaths = $viewPaths;
7789
$this->cachePath = $cachePath;
78-
$this->createCacheDirectory = $createCacheDirectory;
7990
$this->container = new Container();
8091

8192
if (is_multisite()) {
@@ -92,15 +103,15 @@ public function __construct($viewPaths, $cachePath, $createCacheDirectory = true
92103
return $this->dispatcher;
93104
}, true);
94105

95-
$this->container->bindIf('config', function () {
106+
$this->container->bindIf('config', function () use ($createCacheDirectory, $viewPaths) {
96107
$this->cleanCacheDirectory();
97108

98-
if ($this->createCacheDirectory && !$this->filesystem->isDirectory($this->cachePath)) {
109+
if ($createCacheDirectory && !$this->filesystem->isDirectory($this->cachePath)) {
99110
$this->filesystem->makeDirectory($this->cachePath, 0775, true, true);
100111
}
101112

102113
return [
103-
'view.paths' => (array) $this->viewPaths,
114+
'view.paths' => (array) $viewPaths,
104115
'view.compiled' => $this->cachePath,
105116
];
106117
}, true);
@@ -120,7 +131,7 @@ public function __construct($viewPaths, $cachePath, $createCacheDirectory = true
120131
*
121132
* @return mixed
122133
*/
123-
public function __call($name, $arguments)
134+
public function __call(string $name, array $arguments)
124135
{
125136
if (method_exists($this->compiler, $name)) {
126137
return $this->compiler->{$name}(...$arguments);
@@ -129,6 +140,8 @@ public function __call($name, $arguments)
129140
if (method_exists($this->container['view'], $name)) {
130141
return $this->container['view']->{$name}(...$arguments);
131142
}
143+
144+
return null;
132145
}
133146

134147
public function cleanCacheDirectory()

src/BladeControllerLoader.php

+21-17
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,36 @@ private static function instance(): BladeControllerLoader
5353

5454
public static function dataFromController(string $controllerClass, array $additionalData = []): ?array
5555
{
56-
$class = self::getClassToRun(self::instance()->namespace, $controllerClass);
56+
try {
57+
$class = self::getClassToRun(self::instance()->namespace, $controllerClass);
58+
} catch (\Exception $exception) {
59+
// class not found
60+
throw $exception;
61+
return null;
62+
}
5763

58-
if ($class !== null) {
59-
$container = Container::getInstance();
64+
$container = Container::getInstance();
6065

61-
// Recreate the class so that $post is included
62-
$controller = $container->make($class);
66+
// Recreate the class so that $post is included
67+
$controller = $container->make($class);
6368

64-
// Params
65-
$controller->__setParams();
69+
// Params
70+
$controller->__setParams();
6671

67-
// Lifecycle
68-
$controller->__before();
72+
// Lifecycle
73+
$controller->__before();
6974

70-
// Data
71-
$controller->__setData($additionalData);
75+
// Data
76+
$controller->__setData($additionalData);
7277

73-
// Lifecycle
74-
$controller->__after();
78+
// Lifecycle
79+
$controller->__after();
7580

76-
// Return
77-
return $controller->__getData();
78-
}
81+
// Return
82+
return $controller->__getData();
7983
}
8084

81-
private static function getClassToRun(string $namespace, string $class): ?string
85+
private static function getClassToRun(string $namespace, string $class): string
8286
{
8387
try {
8488
$reflection = new \ReflectionClass($namespace . '\\' . $class);

src/helpers.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @param string $view
3333
* @param array $data
34-
* @param bool $data
34+
* @param bool $echo
3535
*
3636
* @return string
3737
*/
@@ -53,7 +53,8 @@ function blade(string $view, array $data = [], bool $echo = true): string
5353
*
5454
* @param string $view
5555
* @param string $controllerClass
56-
* @param bool $data
56+
* @param array $additionalData
57+
* @param bool $echo
5758
*
5859
* @return string
5960
*/

tests/BladeTest.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
use App\Controllers\Variable;
2727
use App\Controllers\VariableDisabled;
2828
use Fiskhandlarn\Blade;
29+
use Fiskhandlarn\BladeControllerLoader;
2930
use Fiskhandlarn\BladeFacade;
3031
use Illuminate\Filesystem\Filesystem;
32+
use WP_Mock;
3133
use WP_Mock\Tools\TestCase;
3234

3335
/**
@@ -42,22 +44,22 @@ class BladeTest extends TestCase
4244
public function setUp(): void
4345
{
4446
parent::setUp();
45-
\WP_Mock::setUp();
47+
WP_Mock::setUp();
4648

4749
$this->blade = new Blade('tests/views', 'tests/cache');
4850

49-
\WP_Mock::onFilter('blade/view/paths')
51+
WP_Mock::onFilter('blade/view/paths') /* @phpstan-ignore-line */
5052
->with(BladeFacade::basePath('resources/views'))
5153
->reply('tests/views');
5254

53-
\WP_Mock::onFilter('blade/cache/path')
55+
WP_Mock::onFilter('blade/cache/path') /* @phpstan-ignore-line */
5456
->with(BladeFacade::basePath('storage/views'))
5557
->reply('tests/cache');
5658
}
5759

5860
public function tearDown(): void
5961
{
60-
\WP_Mock::tearDown();
62+
WP_Mock::tearDown();
6163
parent::tearDown();
6264
}
6365

@@ -66,21 +68,17 @@ public function testInstance()
6668
$this->assertInstanceOf(Blade::class, $this->blade);
6769
}
6870

69-
public function testDefaults()
71+
public function testNullResponses()
7072
{
71-
$this->assertEquals(
72-
'tests/views',
73-
$this->blade->viewPaths
73+
$this->assertNull(
74+
$this->blade->thisMethodDoesNotExists()
7475
);
7576

76-
$this->assertEquals(
77-
'tests/cache/1',
78-
$this->blade->cachePath
79-
);
77+
$this->expectException(\Exception::class);
78+
$this->expectExceptionMessage('No such class found in namespace App\Controllers: NonExistingController');
8079

81-
$this->assertEquals(
82-
true,
83-
$this->blade->createCacheDirectory
80+
$this->assertNull(
81+
BladeControllerLoader::dataFromController('NonExistingController')
8482
);
8583
}
8684

0 commit comments

Comments
 (0)