Skip to content

Commit

Permalink
fix: add types to View $filters and $plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Aug 31, 2023
1 parent f7e6cff commit 5174ed7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
12 changes: 10 additions & 2 deletions app/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
use CodeIgniter\Config\View as BaseView;
use CodeIgniter\View\ViewDecoratorInterface;

/**
* @phpstan-type ParserCallable (callable(mixed): mixed)
* @psalm-type ParserCallable = (callable(mixed): mixed)
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
* @psalm-type ParserCallableString = (callable(mixed): mixed)&string
*/
class View extends BaseView
{
/**
Expand All @@ -30,7 +36,8 @@ class View extends BaseView
* { title|esc(js) }
* { created_on|date(Y-m-d)|esc(attr) }
*
* @var array
* @var array<string, string>
* @phpstan-var array<string, ParserCallableString>
*/
public $filters = [];

Expand All @@ -39,7 +46,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<string, array<string>|callable|string>
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
*/
public $plugins = [];

Expand Down
35 changes: 0 additions & 35 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
$ignoreErrors[] = [
'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\\.$#',
'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\\<string, callable\\-string\\> 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions system/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

/**
* View configuration
*
* @phpstan-type ParserCallable (callable(mixed): mixed)
* @psalm-type ParserCallable = (callable(mixed): mixed)
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
* @psalm-type ParserCallableString = (callable(mixed): mixed)&string
*/
class View extends BaseConfig
{
Expand All @@ -35,7 +40,7 @@ class View extends BaseConfig
* in order for them to be available for use within the Parser.
*
* @var array<string, string>
* @phpstan-var array<string, callable-string>
* @phpstan-var array<string, ParserCallableString>
*/
public $filters = [];

Expand All @@ -44,16 +49,16 @@ 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<string, string>
* @phpstan-var array<string, callable-string>
* @var array<string, array<string>|callable|string>
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
*/
public $plugins = [];

/**
* Built-in View filters.
*
* @var array<string, string>
* @phpstan-var array<string, callable-string>
* @phpstan-var array<string, ParserCallableString>
*/
protected $coreFilters = [
'abs' => '\abs',
Expand Down Expand Up @@ -82,8 +87,8 @@ class View extends BaseConfig
/**
* Built-in View plugins.
*
* @var array<string, string>
* @phpstan-var array<string, callable-string>
* @var array<string, array<string>|callable|string>
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
*/
protected $corePlugins = [
'csp_script_nonce' => '\CodeIgniter\View\Plugins::cspScriptNonce',
Expand Down
10 changes: 8 additions & 2 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

/**
* Class for parsing pseudo-vars
*
* @phpstan-type ParserCallable (callable(mixed): mixed)
* @psalm-type ParserCallable = (callable(mixed): mixed)
* @phpstan-type ParserCallableString (callable(mixed): mixed)&string
* @psalm-type ParserCallableString = (callable(mixed): mixed)&string
*/
class Parser extends View
{
Expand Down Expand Up @@ -58,7 +63,8 @@ class Parser extends View
/**
* Stores any plugins registered at run-time.
*
* @var array
* @var array<string, array<string>|callable|string>
* @phpstan-var array<string, array<ParserCallableString>|ParserCallableString|ParserCallable>
*/
protected $plugins = [];

Expand All @@ -78,7 +84,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);
}
Expand Down

0 comments on commit 5174ed7

Please sign in to comment.