Skip to content

Commit ea425e2

Browse files
committed
fix: add types to View $filters and $plugins
1 parent b582d61 commit ea425e2

File tree

5 files changed

+100
-46
lines changed

5 files changed

+100
-46
lines changed

app/Config/View.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use CodeIgniter\Config\View as BaseView;
66
use CodeIgniter\View\ViewDecoratorInterface;
77

8+
/**
9+
* @phpstan-type ParserCallable (callable(mixed): mixed)
10+
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
11+
*/
812
class View extends BaseView
913
{
1014
/**
@@ -30,7 +34,8 @@ class View extends BaseView
3034
* { title|esc(js) }
3135
* { created_on|date(Y-m-d)|esc(attr) }
3236
*
33-
* @var array
37+
* @var array<string, string>
38+
* @phpstan-var array<string, ParserCallableString>
3439
*/
3540
public $filters = [];
3641

@@ -39,7 +44,8 @@ class View extends BaseView
3944
* by the core Parser by creating aliases that will be replaced with
4045
* any callable. Can be single or tag pair.
4146
*
42-
* @var array
47+
* @var array<string, array<string>|callable|string>
48+
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
4349
*/
4450
public $plugins = [];
4551

phpstan-baseline.php

-35
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
<?php declare(strict_types = 1);
22

33
$ignoreErrors = [];
4-
$ignoreErrors[] = [
5-
'message' => '#^PHPDoc type array of property Config\\\\View\\:\\:\\$filters is not covariant with PHPDoc type array\\<string, callable\\-string\\> of overridden property CodeIgniter\\\\Config\\\\View\\:\\:\\$filters\\.$#',
6-
'count' => 1,
7-
'path' => __DIR__ . '/app/Config/View.php',
8-
];
9-
$ignoreErrors[] = [
10-
'message' => '#^PHPDoc type array of property Config\\\\View\\:\\:\\$plugins is not covariant with PHPDoc type array\\<string, callable\\-string\\> of overridden property CodeIgniter\\\\Config\\\\View\\:\\:\\$plugins\\.$#',
11-
'count' => 1,
12-
'path' => __DIR__ . '/app/Config/View.php',
13-
];
144
$ignoreErrors[] = [
155
'message' => '#^Method CodeIgniter\\\\BaseModel\\:\\:chunk\\(\\) has parameter \\$userFunc with no signature specified for Closure\\.$#',
166
'count' => 1,
@@ -256,26 +246,6 @@
256246
'count' => 1,
257247
'path' => __DIR__ . '/system/ComposerScripts.php',
258248
];
259-
$ignoreErrors[] = [
260-
'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$coreFilters type has no signature specified for callable\\.$#',
261-
'count' => 1,
262-
'path' => __DIR__ . '/system/Config/View.php',
263-
];
264-
$ignoreErrors[] = [
265-
'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$corePlugins type has no signature specified for callable\\.$#',
266-
'count' => 1,
267-
'path' => __DIR__ . '/system/Config/View.php',
268-
];
269-
$ignoreErrors[] = [
270-
'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$filters type has no signature specified for callable\\.$#',
271-
'count' => 1,
272-
'path' => __DIR__ . '/system/Config/View.php',
273-
];
274-
$ignoreErrors[] = [
275-
'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$plugins type has no signature specified for callable\\.$#',
276-
'count' => 1,
277-
'path' => __DIR__ . '/system/Config/View.php',
278-
];
279249
$ignoreErrors[] = [
280250
'message' => '#^Method CodeIgniter\\\\Database\\\\BaseBuilder\\:\\:_whereIn\\(\\) has parameter \\$values with no signature specified for Closure\\.$#',
281251
'count' => 1,
@@ -1706,11 +1676,6 @@
17061676
'count' => 1,
17071677
'path' => __DIR__ . '/system/View/Parser.php',
17081678
];
1709-
$ignoreErrors[] = [
1710-
'message' => '#^Property Config\\\\View\\:\\:\\$plugins \\(array\\) on left side of \\?\\? is not nullable\\.$#',
1711-
'count' => 1,
1712-
'path' => __DIR__ . '/system/View/Parser.php',
1713-
];
17141679
$ignoreErrors[] = [
17151680
'message' => '#^Property CodeIgniter\\\\View\\\\Table\\:\\:\\$function type has no signature specified for callable\\.$#',
17161681
'count' => 1,

psalm-baseline.xml

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
2+
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
3+
<file src="app/Config/View.php">
4+
<UndefinedDocblockClass>
5+
<code><![CDATA[array<string, ParserCallableString>]]></code>
6+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
7+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
8+
<code>public $filters = [];</code>
9+
<code>public $plugins = [];</code>
10+
<code>public $plugins = [];</code>
11+
</UndefinedDocblockClass>
12+
</file>
313
<file src="system/Cache/FactoriesCache/FileVarExportHandler.php">
414
<UndefinedVariable>
515
<code>$val</code>
@@ -27,6 +37,60 @@
2737
<code>$routeWithoutController</code>
2838
</DuplicateArrayKey>
2939
</file>
40+
<file src="system/Config/View.php">
41+
<UndefinedDocblockClass>
42+
<code><![CDATA[array<string, ParserCallableString>]]></code>
43+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
44+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
45+
<code><![CDATA[protected $coreFilters = [
46+
'abs' => '\abs',
47+
'capitalize' => '\CodeIgniter\View\Filters::capitalize',
48+
'date' => '\CodeIgniter\View\Filters::date',
49+
'date_modify' => '\CodeIgniter\View\Filters::date_modify',
50+
'default' => '\CodeIgniter\View\Filters::default',
51+
'esc' => '\CodeIgniter\View\Filters::esc',
52+
'excerpt' => '\CodeIgniter\View\Filters::excerpt',
53+
'highlight' => '\CodeIgniter\View\Filters::highlight',
54+
'highlight_code' => '\CodeIgniter\View\Filters::highlight_code',
55+
'limit_words' => '\CodeIgniter\View\Filters::limit_words',
56+
'limit_chars' => '\CodeIgniter\View\Filters::limit_chars',
57+
'local_currency' => '\CodeIgniter\View\Filters::local_currency',
58+
'local_number' => '\CodeIgniter\View\Filters::local_number',
59+
'lower' => '\strtolower',
60+
'nl2br' => '\CodeIgniter\View\Filters::nl2br',
61+
'number_format' => '\number_format',
62+
'prose' => '\CodeIgniter\View\Filters::prose',
63+
'round' => '\CodeIgniter\View\Filters::round',
64+
'strip_tags' => '\strip_tags',
65+
'title' => '\CodeIgniter\View\Filters::title',
66+
'upper' => '\strtoupper',
67+
];]]></code>
68+
<code><![CDATA[protected $corePlugins = [
69+
'csp_script_nonce' => '\CodeIgniter\View\Plugins::cspScriptNonce',
70+
'csp_style_nonce' => '\CodeIgniter\View\Plugins::cspStyleNonce',
71+
'current_url' => '\CodeIgniter\View\Plugins::currentURL',
72+
'previous_url' => '\CodeIgniter\View\Plugins::previousURL',
73+
'mailto' => '\CodeIgniter\View\Plugins::mailto',
74+
'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto',
75+
'lang' => '\CodeIgniter\View\Plugins::lang',
76+
'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors',
77+
'route' => '\CodeIgniter\View\Plugins::route',
78+
'siteURL' => '\CodeIgniter\View\Plugins::siteURL',
79+
];]]></code>
80+
<code><![CDATA[protected $corePlugins = [
81+
'csp_script_nonce' => '\CodeIgniter\View\Plugins::cspScriptNonce',
82+
'csp_style_nonce' => '\CodeIgniter\View\Plugins::cspStyleNonce',
83+
'current_url' => '\CodeIgniter\View\Plugins::currentURL',
84+
'previous_url' => '\CodeIgniter\View\Plugins::previousURL',
85+
'mailto' => '\CodeIgniter\View\Plugins::mailto',
86+
'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto',
87+
'lang' => '\CodeIgniter\View\Plugins::lang',
88+
'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors',
89+
'route' => '\CodeIgniter\View\Plugins::route',
90+
'siteURL' => '\CodeIgniter\View\Plugins::siteURL',
91+
];]]></code>
92+
</UndefinedDocblockClass>
93+
</file>
3094
<file src="system/Database/BasePreparedQuery.php">
3195
<InaccessibleProperty>
3296
<code><![CDATA[$this->db->transStatus]]></code>
@@ -78,6 +142,14 @@
78142
<code><![CDATA[$this->dom = &$this->domParser]]></code>
79143
</UnsupportedPropertyReferenceUsage>
80144
</file>
145+
<file src="system/View/Parser.php">
146+
<UndefinedDocblockClass>
147+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
148+
<code><![CDATA[array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>]]></code>
149+
<code>protected $plugins = [];</code>
150+
<code>protected $plugins = [];</code>
151+
</UndefinedDocblockClass>
152+
</file>
81153
<file src="tests/_support/Config/Filters.php">
82154
<UndefinedGlobalVariable>
83155
<code>$filters</code>

system/Config/View.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
/**
1717
* View configuration
18+
*
19+
* @phpstan-type ParserCallable (callable(mixed): mixed)
20+
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
1821
*/
1922
class View extends BaseConfig
2023
{
@@ -34,8 +37,10 @@ class View extends BaseConfig
3437
* To prevent potential abuse, all filters MUST be defined here
3538
* in order for them to be available for use within the Parser.
3639
*
40+
* @psalm-suppress UndefinedDocblockClass
41+
*
3742
* @var array<string, string>
38-
* @phpstan-var array<string, callable-string>
43+
* @phpstan-var array<string, ParserCallableString>
3944
*/
4045
public $filters = [];
4146

@@ -44,16 +49,18 @@ class View extends BaseConfig
4449
* by the core Parser by creating aliases that will be replaced with
4550
* any callable. Can be single or tag pair.
4651
*
47-
* @var array<string, string>
48-
* @phpstan-var array<string, callable-string>
52+
* @psalm-suppress UndefinedDocblockClass
53+
*
54+
* @var array<string, array<string>|callable|string>
55+
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
4956
*/
5057
public $plugins = [];
5158

5259
/**
5360
* Built-in View filters.
5461
*
5562
* @var array<string, string>
56-
* @phpstan-var array<string, callable-string>
63+
* @phpstan-var array<string, ParserCallableString>
5764
*/
5865
protected $coreFilters = [
5966
'abs' => '\abs',
@@ -82,8 +89,8 @@ class View extends BaseConfig
8289
/**
8390
* Built-in View plugins.
8491
*
85-
* @var array<string, string>
86-
* @phpstan-var array<string, callable-string>
92+
* @var array<string, array<string>|callable|string>
93+
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
8794
*/
8895
protected $corePlugins = [
8996
'csp_script_nonce' => '\CodeIgniter\View\Plugins::cspScriptNonce',

system/View/Parser.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
/**
2121
* Class for parsing pseudo-vars
22+
*
23+
* @phpstan-type ParserCallable (callable(mixed): mixed)
24+
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
2225
*/
2326
class Parser extends View
2427
{
@@ -58,7 +61,8 @@ class Parser extends View
5861
/**
5962
* Stores any plugins registered at run-time.
6063
*
61-
* @var array
64+
* @var array<string, array<string>|callable|string>
65+
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
6266
*/
6367
protected $plugins = [];
6468

@@ -78,7 +82,7 @@ class Parser extends View
7882
public function __construct(ViewConfig $config, ?string $viewPath = null, $loader = null, ?bool $debug = null, ?LoggerInterface $logger = null)
7983
{
8084
// Ensure user plugins override core plugins.
81-
$this->plugins = $config->plugins ?? [];
85+
$this->plugins = $config->plugins;
8286

8387
parent::__construct($config, $viewPath, $loader, $debug, $logger);
8488
}

0 commit comments

Comments
 (0)