Skip to content

Commit 46ac972

Browse files
authored
Merge pull request #8208 from kenjis/refactor-View-classes
refactor: View classes to fix PHPStan errors
2 parents 6a7fd1e + 6ddc920 commit 46ac972

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,16 +3951,6 @@
39513951
'count' => 1,
39523952
'path' => __DIR__ . '/system/View/Cell.php',
39533953
];
3954-
$ignoreErrors[] = [
3955-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
3956-
'count' => 8,
3957-
'path' => __DIR__ . '/system/View/Cell.php',
3958-
];
3959-
$ignoreErrors[] = [
3960-
'message' => '#^Property CodeIgniter\\\\View\\\\Cell\\:\\:\\$cache \\(CodeIgniter\\\\Cache\\\\CacheInterface\\) in empty\\(\\) is not falsy\\.$#',
3961-
'count' => 2,
3962-
'path' => __DIR__ . '/system/View/Cell.php',
3963-
];
39643954
$ignoreErrors[] = [
39653955
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
39663956
'count' => 1,
@@ -4006,16 +3996,6 @@
40063996
'count' => 3,
40073997
'path' => __DIR__ . '/system/View/View.php',
40083998
];
4009-
$ignoreErrors[] = [
4010-
'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\\(\\)$#',
4011-
'count' => 1,
4012-
'path' => __DIR__ . '/system/View/View.php',
4013-
];
4014-
$ignoreErrors[] = [
4015-
'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\\(\\)$#',
4016-
'count' => 1,
4017-
'path' => __DIR__ . '/system/View/View.php',
4018-
];
40193999
$ignoreErrors[] = [
40204000
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
40214001
'count' => 2,

system/View/Cell.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
8484
$params = $this->prepareParams($params);
8585

8686
// Is the output cached?
87-
$cacheName = ! empty($cacheName)
88-
? $cacheName
89-
: str_replace(['\\', '/'], '', $class) . $method . md5(serialize($params));
87+
$cacheName ??= str_replace(['\\', '/'], '', $class) . $method . md5(serialize($params));
9088

91-
if (! empty($this->cache) && $output = $this->cache->get($cacheName)) {
89+
if ($output = $this->cache->get($cacheName)) {
9290
return $output;
9391
}
9492

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

107105
// Can we cache it?
108-
if (! empty($this->cache) && $ttl !== 0) {
106+
if ($ttl !== 0) {
109107
$this->cache->save($cacheName, $output, $ttl);
110108
}
111109

@@ -119,11 +117,14 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
119117
*
120118
* @param array|string|null $params
121119
*
122-
* @return array|null
120+
* @return array
123121
*/
124122
public function prepareParams($params)
125123
{
126-
if (empty($params) || (! is_string($params) && ! is_array($params))) {
124+
if (
125+
($params === null || $params === '' || $params === [])
126+
|| (! is_string($params) && ! is_array($params))
127+
) {
127128
return [];
128129
}
129130

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

141142
foreach ($params as $p) {
142-
if (! empty($p)) {
143+
if ($p !== '') {
143144
[$key, $val] = explode('=', $p);
144145

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

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

178-
if (empty($class)) {
179+
if ($class === '') {
179180
throw ViewException::forNoCellClass();
180181
}
181182

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

190-
if (empty($method)) {
191+
if ($method === '') {
191192
$method = 'index';
192193
}
193194

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

276277
if ($paramCount === 0) {
277-
if (! empty($params)) {
278+
if ($params !== []) {
278279
throw ViewException::forMissingCellParameters($class, $method);
279280
}
280281

system/View/RendererInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function renderString(string $view, ?array $options = null, bool $saveDat
4646
*
4747
* @param string $context The context to escape it for: html, css, js, url
4848
* If 'raw', no escaping will happen
49+
* @phpstan-param null|'html'|'js'|'css'|'url'|'attr'|'raw' $context
4950
*
5051
* @return RendererInterface
5152
*/
@@ -57,6 +58,7 @@ public function setData(array $data = [], ?string $context = null);
5758
* @param mixed $value
5859
* @param string $context The context to escape it for: html, css, js, url
5960
* If 'raw' no escaping will happen
61+
* @phpstan-param null|'html'|'js'|'css'|'url'|'attr'|'raw' $context
6062
*
6163
* @return RendererInterface
6264
*/

0 commit comments

Comments
 (0)