From ea425e29862f89bba90e265e1986eaf1bf9517e1 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Thu, 31 Aug 2023 13:37:45 +0800 Subject: [PATCH] fix: add types to View $filters and $plugins --- app/Config/View.php | 10 ++++-- phpstan-baseline.php | 35 -------------------- psalm-baseline.xml | 74 +++++++++++++++++++++++++++++++++++++++++- system/Config/View.php | 19 +++++++---- system/View/Parser.php | 8 +++-- 5 files changed, 100 insertions(+), 46 deletions(-) diff --git a/app/Config/View.php b/app/Config/View.php index 78cd547e3b8f..cf00863fb70b 100644 --- a/app/Config/View.php +++ b/app/Config/View.php @@ -5,6 +5,10 @@ use CodeIgniter\Config\View as BaseView; use CodeIgniter\View\ViewDecoratorInterface; +/** + * @phpstan-type ParserCallable (callable(mixed): mixed) + * @phpstan-type ParserCallableString (callable(mixed): mixed)&string + */ class View extends BaseView { /** @@ -30,7 +34,8 @@ class View extends BaseView * { title|esc(js) } * { created_on|date(Y-m-d)|esc(attr) } * - * @var array + * @var array + * @phpstan-var array */ public $filters = []; @@ -39,7 +44,8 @@ class View extends BaseView * by the core Parser by creating aliases that will be replaced with * any callable. Can be single or tag pair. * - * @var array + * @var array|callable|string> + * @phpstan-var array|ParserCallableString|ParserCallable> */ public $plugins = []; diff --git a/phpstan-baseline.php b/phpstan-baseline.php index ec5d6987990e..e3267ecafdca 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -1,16 +1,6 @@ '#^PHPDoc type array of property Config\\\\View\\:\\:\\$filters is not covariant with PHPDoc type array\\ of overridden property CodeIgniter\\\\Config\\\\View\\:\\:\\$filters\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/app/Config/View.php', -]; -$ignoreErrors[] = [ - 'message' => '#^PHPDoc type array of property Config\\\\View\\:\\:\\$plugins is not covariant with PHPDoc type array\\ of overridden property CodeIgniter\\\\Config\\\\View\\:\\:\\$plugins\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/app/Config/View.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\BaseModel\\:\\:chunk\\(\\) has parameter \\$userFunc with no signature specified for Closure\\.$#', 'count' => 1, @@ -256,26 +246,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/ComposerScripts.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$coreFilters type has no signature specified for callable\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/View.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$corePlugins type has no signature specified for callable\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/View.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$filters type has no signature specified for callable\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/View.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Property CodeIgniter\\\\Config\\\\View\\:\\:\\$plugins type has no signature specified for callable\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/View.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Database\\\\BaseBuilder\\:\\:_whereIn\\(\\) has parameter \\$values with no signature specified for Closure\\.$#', 'count' => 1, @@ -1706,11 +1676,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/View/Parser.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Property Config\\\\View\\:\\:\\$plugins \\(array\\) on left side of \\?\\? is not nullable\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/View/Parser.php', -]; $ignoreErrors[] = [ 'message' => '#^Property CodeIgniter\\\\View\\\\Table\\:\\:\\$function type has no signature specified for callable\\.$#', 'count' => 1, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3ede420d002c..379314e5093a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,15 @@ - + + + + ]]> + |ParserCallableString|ParserCallable>]]> + |ParserCallableString|ParserCallable>]]> + public $filters = []; + public $plugins = []; + public $plugins = []; + + $val @@ -27,6 +37,60 @@ $routeWithoutController + + + ]]> + |ParserCallableString|ParserCallable>]]> + |ParserCallableString|ParserCallable>]]> + '\abs', + 'capitalize' => '\CodeIgniter\View\Filters::capitalize', + 'date' => '\CodeIgniter\View\Filters::date', + 'date_modify' => '\CodeIgniter\View\Filters::date_modify', + 'default' => '\CodeIgniter\View\Filters::default', + 'esc' => '\CodeIgniter\View\Filters::esc', + 'excerpt' => '\CodeIgniter\View\Filters::excerpt', + 'highlight' => '\CodeIgniter\View\Filters::highlight', + 'highlight_code' => '\CodeIgniter\View\Filters::highlight_code', + 'limit_words' => '\CodeIgniter\View\Filters::limit_words', + 'limit_chars' => '\CodeIgniter\View\Filters::limit_chars', + 'local_currency' => '\CodeIgniter\View\Filters::local_currency', + 'local_number' => '\CodeIgniter\View\Filters::local_number', + 'lower' => '\strtolower', + 'nl2br' => '\CodeIgniter\View\Filters::nl2br', + 'number_format' => '\number_format', + 'prose' => '\CodeIgniter\View\Filters::prose', + 'round' => '\CodeIgniter\View\Filters::round', + 'strip_tags' => '\strip_tags', + 'title' => '\CodeIgniter\View\Filters::title', + 'upper' => '\strtoupper', + ];]]> + '\CodeIgniter\View\Plugins::cspScriptNonce', + 'csp_style_nonce' => '\CodeIgniter\View\Plugins::cspStyleNonce', + 'current_url' => '\CodeIgniter\View\Plugins::currentURL', + 'previous_url' => '\CodeIgniter\View\Plugins::previousURL', + 'mailto' => '\CodeIgniter\View\Plugins::mailto', + 'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto', + 'lang' => '\CodeIgniter\View\Plugins::lang', + 'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors', + 'route' => '\CodeIgniter\View\Plugins::route', + 'siteURL' => '\CodeIgniter\View\Plugins::siteURL', + ];]]> + '\CodeIgniter\View\Plugins::cspScriptNonce', + 'csp_style_nonce' => '\CodeIgniter\View\Plugins::cspStyleNonce', + 'current_url' => '\CodeIgniter\View\Plugins::currentURL', + 'previous_url' => '\CodeIgniter\View\Plugins::previousURL', + 'mailto' => '\CodeIgniter\View\Plugins::mailto', + 'safe_mailto' => '\CodeIgniter\View\Plugins::safeMailto', + 'lang' => '\CodeIgniter\View\Plugins::lang', + 'validation_errors' => '\CodeIgniter\View\Plugins::validationErrors', + 'route' => '\CodeIgniter\View\Plugins::route', + 'siteURL' => '\CodeIgniter\View\Plugins::siteURL', + ];]]> + + db->transStatus]]> @@ -78,6 +142,14 @@ dom = &$this->domParser]]> + + + |ParserCallableString|ParserCallable>]]> + |ParserCallableString|ParserCallable>]]> + protected $plugins = []; + protected $plugins = []; + + $filters diff --git a/system/Config/View.php b/system/Config/View.php index de4ed62b936a..5a6a6404b69e 100644 --- a/system/Config/View.php +++ b/system/Config/View.php @@ -15,6 +15,9 @@ /** * View configuration + * + * @phpstan-type ParserCallable (callable(mixed): mixed) + * @phpstan-type ParserCallableString (callable(mixed): mixed)&string */ class View extends BaseConfig { @@ -34,8 +37,10 @@ class View extends BaseConfig * To prevent potential abuse, all filters MUST be defined here * in order for them to be available for use within the Parser. * + * @psalm-suppress UndefinedDocblockClass + * * @var array - * @phpstan-var array + * @phpstan-var array */ public $filters = []; @@ -44,8 +49,10 @@ class View extends BaseConfig * by the core Parser by creating aliases that will be replaced with * any callable. Can be single or tag pair. * - * @var array - * @phpstan-var array + * @psalm-suppress UndefinedDocblockClass + * + * @var array|callable|string> + * @phpstan-var array|ParserCallableString|ParserCallable> */ public $plugins = []; @@ -53,7 +60,7 @@ class View extends BaseConfig * Built-in View filters. * * @var array - * @phpstan-var array + * @phpstan-var array */ protected $coreFilters = [ 'abs' => '\abs', @@ -82,8 +89,8 @@ class View extends BaseConfig /** * Built-in View plugins. * - * @var array - * @phpstan-var array + * @var array|callable|string> + * @phpstan-var array|ParserCallableString|ParserCallable> */ protected $corePlugins = [ 'csp_script_nonce' => '\CodeIgniter\View\Plugins::cspScriptNonce', diff --git a/system/View/Parser.php b/system/View/Parser.php index b9abf91edb5f..84ff821540cb 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -19,6 +19,9 @@ /** * Class for parsing pseudo-vars + * + * @phpstan-type ParserCallable (callable(mixed): mixed) + * @phpstan-type ParserCallableString (callable(mixed): mixed)&string */ class Parser extends View { @@ -58,7 +61,8 @@ class Parser extends View /** * Stores any plugins registered at run-time. * - * @var array + * @var array|callable|string> + * @phpstan-var array|ParserCallableString|ParserCallable> */ protected $plugins = []; @@ -78,7 +82,7 @@ class Parser extends View public function __construct(ViewConfig $config, ?string $viewPath = null, $loader = null, ?bool $debug = null, ?LoggerInterface $logger = null) { // Ensure user plugins override core plugins. - $this->plugins = $config->plugins ?? []; + $this->plugins = $config->plugins; parent::__construct($config, $viewPath, $loader, $debug, $logger); }