Skip to content

Commit

Permalink
Merge pull request #8208 from kenjis/refactor-View-classes
Browse files Browse the repository at this point in the history
refactor: View classes to fix PHPStan errors
  • Loading branch information
kenjis authored Nov 16, 2023
2 parents 6a7fd1e + 6ddc920 commit 46ac972
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3951,16 +3951,6 @@
'count' => 1,
'path' => __DIR__ . '/system/View/Cell.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 8,
'path' => __DIR__ . '/system/View/Cell.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\View\\\\Cell\\:\\:\\$cache \\(CodeIgniter\\\\Cache\\\\CacheInterface\\) in empty\\(\\) is not falsy\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/View/Cell.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 1,
Expand Down Expand Up @@ -4006,16 +3996,6 @@
'count' => 3,
'path' => __DIR__ . '/system/View/View.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#2 \\$context \\(\'attr\'\\|\'css\'\\|\'html\'\\|\'js\'\\|\'raw\'\\|\'url\'\\|null\\) of method CodeIgniter\\\\View\\\\View\\:\\:setData\\(\\) should be contravariant with parameter \\$context \\(string\\|null\\) of method CodeIgniter\\\\View\\\\RendererInterface\\:\\:setData\\(\\)$#',
'count' => 1,
'path' => __DIR__ . '/system/View/View.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#3 \\$context \\(\'attr\'\\|\'css\'\\|\'html\'\\|\'js\'\\|\'raw\'\\|\'url\'\\|null\\) of method CodeIgniter\\\\View\\\\View\\:\\:setVar\\(\\) should be contravariant with parameter \\$context \\(string\\|null\\) of method CodeIgniter\\\\View\\\\RendererInterface\\:\\:setVar\\(\\)$#',
'count' => 1,
'path' => __DIR__ . '/system/View/View.php',
];
$ignoreErrors[] = [
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
'count' => 2,
Expand Down
23 changes: 12 additions & 11 deletions system/View/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
$params = $this->prepareParams($params);

// Is the output cached?
$cacheName = ! empty($cacheName)
? $cacheName
: str_replace(['\\', '/'], '', $class) . $method . md5(serialize($params));
$cacheName ??= str_replace(['\\', '/'], '', $class) . $method . md5(serialize($params));

if (! empty($this->cache) && $output = $this->cache->get($cacheName)) {
if ($output = $this->cache->get($cacheName)) {
return $output;
}

Expand All @@ -105,7 +103,7 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
: $this->renderSimpleClass($instance, $method, $params, $class);

// Can we cache it?
if (! empty($this->cache) && $ttl !== 0) {
if ($ttl !== 0) {
$this->cache->save($cacheName, $output, $ttl);
}

Expand All @@ -119,11 +117,14 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
*
* @param array|string|null $params
*
* @return array|null
* @return array
*/
public function prepareParams($params)
{
if (empty($params) || (! is_string($params) && ! is_array($params))) {
if (
($params === null || $params === '' || $params === [])
|| (! is_string($params) && ! is_array($params))
) {
return [];
}

Expand All @@ -139,7 +140,7 @@ public function prepareParams($params)
unset($separator);

foreach ($params as $p) {
if (! empty($p)) {
if ($p !== '') {
[$key, $val] = explode('=', $p);

$newParams[trim($key)] = trim($val, ', ');
Expand Down Expand Up @@ -175,7 +176,7 @@ protected function determineClass(string $library): array

[$class, $method] = explode(':', $library);

if (empty($class)) {
if ($class === '') {
throw ViewException::forNoCellClass();
}

Expand All @@ -187,7 +188,7 @@ protected function determineClass(string $library): array
throw ViewException::forInvalidCellClass($class);
}

if (empty($method)) {
if ($method === '') {
$method = 'index';
}

Expand Down Expand Up @@ -274,7 +275,7 @@ final protected function renderSimpleClass($instance, string $method, array $par
$refParams = $refMethod->getParameters();

if ($paramCount === 0) {
if (! empty($params)) {
if ($params !== []) {
throw ViewException::forMissingCellParameters($class, $method);
}

Expand Down
2 changes: 2 additions & 0 deletions system/View/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function renderString(string $view, ?array $options = null, bool $saveDat
*
* @param string $context The context to escape it for: html, css, js, url
* If 'raw', no escaping will happen
* @phpstan-param null|'html'|'js'|'css'|'url'|'attr'|'raw' $context
*
* @return RendererInterface
*/
Expand All @@ -57,6 +58,7 @@ public function setData(array $data = [], ?string $context = null);
* @param mixed $value
* @param string $context The context to escape it for: html, css, js, url
* If 'raw' no escaping will happen
* @phpstan-param null|'html'|'js'|'css'|'url'|'attr'|'raw' $context
*
* @return RendererInterface
*/
Expand Down

0 comments on commit 46ac972

Please sign in to comment.