Skip to content

Commit dea8438

Browse files
committed
refactor: fix phpstan errors in View
1 parent 18804c5 commit dea8438

15 files changed

Lines changed: 67 additions & 182 deletions

‎system/Config/Factories.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* large performance boost and helps keep code clean of lengthy
2727
* instantiation checks.
2828
*
29+
* @method static object|null cells(string $alias, array<string, bool|string|null> $options = [])
2930
* @method static BaseConfig|null config(...$arguments)
3031
* @method static Model|null models(string $alias, array<string, bool|string|null> $options = [], ?ConnectionInterface &$conn = null)
3132
* @see \CodeIgniter\Config\FactoriesTest

‎system/Traits/PropertiesTrait.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ trait PropertiesTrait
2727
{
2828
/**
2929
* Attempts to set the values of public class properties.
30+
*
31+
* @param array<string, mixed> $params
3032
*/
3133
final public function fill(array $params): self
3234
{
@@ -61,6 +63,8 @@ public function getProperties(object $obj): array
6163

6264
/**
6365
* Get the protected and private properties of the class and return as an array.
66+
*
67+
* @return list<ReflectionProperty>
6468
*/
6569
final public function getNonPublicProperties(): array
6670
{

‎system/View/Cell.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public function render(string $library, $params = null, int $ttl = 0, ?string $c
7878
{
7979
[$instance, $method] = $this->determineClass($library);
8080

81-
$class = is_object($instance)
82-
? $instance::class
83-
: null;
81+
$class = $instance::class;
8482

8583
$params = $this->prepareParams($params);
8684

@@ -160,6 +158,8 @@ public function prepareParams($params)
160158
/**
161159
* Given the library string, attempts to determine the class and method
162160
* to call.
161+
*
162+
* @return array{object, string}
163163
*/
164164
protected function determineClass(string $library): array
165165
{
@@ -198,6 +198,8 @@ protected function determineClass(string $library): array
198198

199199
/**
200200
* Renders a cell that extends the BaseCell class.
201+
*
202+
* @param array<string, mixed> $params
201203
*/
202204
final protected function renderCell(BaseCell $instance, string $method, array $params): string
203205
{
@@ -234,6 +236,10 @@ final protected function renderCell(BaseCell $instance, string $method, array $p
234236
* Returns the values from $params that match the parameters
235237
* for a method, in the order they are defined. This allows
236238
* them to be passed directly into the method.
239+
*
240+
* @param array<string, mixed> $params
241+
*
242+
* @return list<mixed>
237243
*/
238244
private function getMethodParams(BaseCell $instance, string $method, array $params): array
239245
{
@@ -262,9 +268,9 @@ private function getMethodParams(BaseCell $instance, string $method, array $para
262268
*
263269
* @todo Determine if this can be refactored to use $this-getMethodParams().
264270
*
265-
* @param object $instance
271+
* @param array<string, mixed> $params
266272
*/
267-
final protected function renderSimpleClass($instance, string $method, array $params, string $class): string
273+
final protected function renderSimpleClass(object $instance, string $method, array $params, string $class): string
268274
{
269275
// Try to match up the parameter list we were provided
270276
// with the parameter name in the callback method.

‎system/View/Cells/Cell.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function setView(string $view)
7171
* current scope and captures the output buffer instead of
7272
* relying on the view service.
7373
*
74+
* @param array<string, mixed> $data
75+
*
7476
* @throws LogicException
7577
*/
7678
final protected function view(?string $view, array $data = []): string
@@ -129,6 +131,10 @@ public function __toString(): string
129131
/**
130132
* Allows the developer to define computed properties
131133
* as methods with `get` prefixed to the protected/private property name.
134+
*
135+
* @param array<string, mixed> $properties
136+
*
137+
* @return array<string, mixed>
132138
*/
133139
private function includeComputedProperties(array $properties): array
134140
{

‎system/View/Parser.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,9 @@ public function setConditionalDelimiters($leftDelimiter = '{', $rightDelimiter =
543543
* Handles replacing a pseudo-variable with the actual content. Will double-check
544544
* for escaping brackets.
545545
*
546-
* @param array|string $pattern
547-
* @param string $content
548-
* @param string $template
546+
* @param array<array-key, string>|string $pattern
547+
* @param string $content
548+
* @param string $template
549549
*/
550550
protected function replaceSingle($pattern, $content, $template, bool $escape = false): string
551551
{

‎tests/system/Config/FactoriesTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ class_alias(SomeWidget::class, $class);
324324

325325
public function testCanLoadTwoCellsWithSameShortName(): void
326326
{
327-
$cell1 = Factories::cells('\\' . SampleClass::class); // @phpstan-ignore staticMethod.notFound
328-
$cell2 = Factories::cells('\\' . \Tests\Support\View\OtherCells\SampleClass::class); // @phpstan-ignore staticMethod.notFound
327+
$cell1 = Factories::cells('\\' . SampleClass::class);
328+
$cell2 = Factories::cells('\\' . \Tests\Support\View\OtherCells\SampleClass::class);
329329

330330
$this->assertNotSame($cell1, $cell2);
331331
}

‎tests/system/View/ParserPluginTest.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ protected function setUp(): void
3636

3737
Services::reset(true);
3838

39-
$this->parser = service('parser');
40-
$this->validator = service('validation');
39+
$this->parser = service('parser');
40+
41+
$validator = service('validation');
42+
$this->assertInstanceOf(Validation::class, $validator);
43+
$this->validator = $validator;
4144
}
4245

4346
public function testCurrentURL(): void

‎tests/system/View/ParserTest.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ public function testEscHandling($value, $expected = null): void
438438
$this->assertSame($expected, \esc($value));
439439
}
440440

441+
/**
442+
* @return iterable<string, list<mixed>>
443+
*/
441444
public static function provideEscHandling(): iterable
442445
{
443446
return [
@@ -543,20 +546,20 @@ public function testNoEscapingSetData(): void
543546
{
544547
$template = '{ foo | noescape}';
545548

546-
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown');
549+
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown'); // @phpstan-ignore argument.type (deliberately invalid context)
547550
$this->assertSame('http://foo.com', $this->parser->renderString($template));
548551
}
549552

550553
public function testAutoEscaping(): void
551554
{
552-
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown');
555+
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown'); // @phpstan-ignore argument.type (deliberately invalid context)
553556

554557
$this->assertSame('html', $this->parser->shouldAddEscaping('{ foo | this | that }'));
555558
}
556559

557560
public function testAutoEscapingNot(): void
558561
{
559-
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown');
562+
$this->parser->setData(['foo' => 'http://foo.com'], 'unknown'); // @phpstan-ignore argument.type (deliberately invalid context)
560563
$this->assertFalse($this->parser->shouldAddEscaping('{ foo | noescape }'));
561564
}
562565

‎tests/system/View/TableTest.php‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function testPrepArgs(): void
201201

202202
$this->assertSame(
203203
$expected,
204-
$this->table->prepArgs(['name', 'color', 'size']),
204+
$this->table->prepArgs(['name', 'color', 'size']), // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
205205
);
206206

207207
// with cell attributes
@@ -213,7 +213,7 @@ public function testPrepArgs(): void
213213

214214
$this->assertSame(
215215
$expected,
216-
$this->table->prepArgs(['name', 'color', 'size', ['data' => 'weight', 'class' => 'awesome']]),
216+
$this->table->prepArgs(['name', 'color', 'size', ['data' => 'weight', 'class' => 'awesome']]), // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
217217
);
218218
}
219219

@@ -241,7 +241,7 @@ public function testDefaultTemplateKeys(): void
241241
];
242242

243243
foreach ($keys as $key) {
244-
$this->assertArrayHasKey($key, $this->table->defaultTemplate());
244+
$this->assertArrayHasKey($key, $this->table->defaultTemplate()); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
245245
}
246246
}
247247

@@ -251,14 +251,14 @@ public function testCompileTemplate(): void
251251

252252
// non default key
253253
$this->table->setTemplate(['nonsense' => 'foo']);
254-
$this->table->compileTemplate();
254+
$this->table->compileTemplate(); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
255255

256256
$this->assertArrayHasKey('nonsense', $this->table->template);
257257
$this->assertSame('foo', $this->table->template['nonsense']);
258258

259259
// override default
260260
$this->table->setTemplate(['table_close' => '</table junk>']);
261-
$this->table->compileTemplate();
261+
$this->table->compileTemplate(); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
262262

263263
$this->assertArrayHasKey('table_close', $this->table->template);
264264
$this->assertSame('</table junk>', $this->table->template['table_close']);
@@ -267,9 +267,9 @@ public function testCompileTemplate(): void
267267
public function testMakeColumns(): void
268268
{
269269
// Test bogus parameters
270-
$this->assertFalse($this->table->makeColumns('invalid_junk'));
270+
$this->assertFalse($this->table->makeColumns('invalid_junk')); // @phpstan-ignore argument.type (deliberately wrong type)
271271
$this->assertFalse($this->table->makeColumns([]));
272-
$this->assertFalse($this->table->makeColumns(['one', 'two'], '2.5'));
272+
$this->assertFalse($this->table->makeColumns(['one', 'two'], '2.5')); // @phpstan-ignore argument.type (deliberately wrong type)
273273

274274
// Now on to the actual column creation
275275

@@ -345,12 +345,12 @@ public function testSetFromArray(): void
345345
];
346346

347347
$this->table->autoHeading = false;
348-
$this->table->setFromArray($data);
348+
$this->table->setFromArray($data); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
349349
$this->assertEmpty($this->table->heading);
350350

351351
$this->table->clear();
352352

353-
$this->table->setFromArray($data);
353+
$this->table->setFromArray($data); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
354354
$this->assertCount(2, $this->table->rows);
355355

356356
$expected = [
@@ -376,9 +376,9 @@ public function testSetFromObject(): void
376376
$dummy->connID = null;
377377
$dummy->resultID = null;
378378

379-
$DBResult = new DBResultDummy($dummy->connID, $dummy->resultID);
379+
$DBResult = new DBResultDummy($dummy->connID, $dummy->resultID); // @phpstan-ignore argument.type, argument.type (deliberately null, exercising the fallback path)
380380

381-
$this->table->setFromDBResult($DBResult);
381+
$this->table->setFromDBResult($DBResult); // @phpstan-ignore method.notFound (Testing MockTable::__call() fallback)
382382

383383
$expected = [
384384
['data' => 'name'],
@@ -733,7 +733,7 @@ public function testGenerateFromDBResult(): void
733733
$dummy = new stdClass();
734734
$dummy->connID = null;
735735
$dummy->resultID = null;
736-
$DBResult = new DBResultDummy($dummy->connID, $dummy->resultID);
736+
$DBResult = new DBResultDummy($dummy->connID, $dummy->resultID); // @phpstan-ignore argument.type, argument.type (deliberately null, exercising the fallback path)
737737

738738
$table = $this->table->generate($DBResult);
739739

@@ -780,6 +780,10 @@ public function testInvalidCallback(): void
780780
$this->assertStringContainsString('<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>', $generated);
781781
}
782782

783+
/**
784+
* @param array<string, string> $heading
785+
* @param array<string, int|string> $row
786+
*/
783787
#[DataProvider('orderedColumnUsecases')]
784788
public function testAddRowAndGenerateOrderedColumns(array $heading, array $row, string $expectContainsString): void
785789
{
@@ -792,6 +796,10 @@ public function testAddRowAndGenerateOrderedColumns(array $heading, array $row,
792796
$this->assertStringContainsString($expectContainsString, $generated);
793797
}
794798

799+
/**
800+
* @param array<string, string> $heading
801+
* @param array<string, int|string> $row
802+
*/
795803
#[DataProvider('orderedColumnUsecases')]
796804
public function testGenerateOrderedColumns(array $heading, array $row, string $expectContainsString): void
797805
{
@@ -803,6 +811,9 @@ public function testGenerateOrderedColumns(array $heading, array $row, string $e
803811
$this->assertStringContainsString($expectContainsString, $generated);
804812
}
805813

814+
/**
815+
* @return iterable<string, array{heading: array<string, string>, row: array<string, int|string>, expectContainsString: string}>
816+
*/
806817
public static function orderedColumnUsecases(): iterable
807818
{
808819
yield from [

‎utils/phpstan-baseline/argument.type.neon‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 21 errors
1+
# total 12 errors
22

33
parameters:
44
ignoreErrors:
@@ -51,28 +51,3 @@ parameters:
5151
message: '#^Parameter \#1 \$format of method CodeIgniter\\RESTful\\ResourceController\:\:setFormat\(\) expects ''json''\|''xml'', ''Nonsense'' given\.$#'
5252
count: 1
5353
path: ../../tests/system/RESTful/ResourceControllerTest.php
54-
55-
-
56-
message: '#^Parameter \#2 \$context of method CodeIgniter\\View\\Parser\:\:setData\(\) expects ''attr''\|''css''\|''html''\|''js''\|''raw''\|''url''\|null, ''unknown'' given\.$#'
57-
count: 3
58-
path: ../../tests/system/View/ParserTest.php
59-
60-
-
61-
message: '#^Parameter \#1 \$array of method CodeIgniter\\View\\Table\:\:makeColumns\(\) expects list\<string\>, ''invalid_junk'' given\.$#'
62-
count: 1
63-
path: ../../tests/system/View/TableTest.php
64-
65-
-
66-
message: '#^Parameter \#1 \$connID of class CodeIgniter\\View\\DBResultDummy constructor expects mysqli, null given\.$#'
67-
count: 2
68-
path: ../../tests/system/View/TableTest.php
69-
70-
-
71-
message: '#^Parameter \#2 \$columnLimit of method CodeIgniter\\View\\Table\:\:makeColumns\(\) expects int, string given\.$#'
72-
count: 1
73-
path: ../../tests/system/View/TableTest.php
74-
75-
-
76-
message: '#^Parameter \#2 \$resultID of class CodeIgniter\\View\\DBResultDummy constructor expects mysqli_result, null given\.$#'
77-
count: 2
78-
path: ../../tests/system/View/TableTest.php

0 commit comments

Comments
 (0)