Skip to content

Commit d5064db

Browse files
committed
address PHPStan issues
1 parent 81f58ff commit d5064db

15 files changed

+73
-91
lines changed

.gitattributes

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
* text eol=lf
22

3-
tests export-ignore
4-
.github export-ignore
5-
.gitattributes export-ignore
6-
.gitignore export-ignore
7-
README.md export-ignore
8-
phpcs.xml.dist export-ignore
9-
phpunit.xml.dist export-ignore
10-
phpstan.neon.dist export-ignore
3+
*.php diff=php
4+
5+
/.github/ export-ignore
6+
/docs/ export-ignore
7+
/tests/ export-ignore
8+
/.gitattributes export-ignore
9+
/.gitignore export-ignore
10+
/README.md export-ignore
11+
/phpcs.xml.dist export-ignore
12+
/phpstan.neon.dist export-ignore
13+
/phpunit.xml.dist export-ignore

phpcs.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<ruleset>
33
<file>./src</file>
44
<file>./tests</file>
5+
<exclude-pattern>./tests/unit/stubs.php</exclude-pattern>
56

67
<arg value="sp"/>
78
<arg name="colors"/>

phpstan.neon.dist

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
includes:
2+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
23
- vendor/phpstan/phpstan-mockery/extension.neon
34
- vendor/phpstan/phpstan-phpunit/extension.neon
4-
- vendor/szepeviktor/phpstan-wordpress/extension.neon
5-
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
65
- vendor/swissspidy/phpstan-no-private/rules.neon
6+
- vendor/szepeviktor/phpstan-wordpress/extension.neon
77
parameters:
88
level: 8
99
paths:
1010
- src/
1111
- tests/
1212
treatPhpDocTypesAsCertain: false
1313
ignoreErrors:
14-
# It seems like PHPStan does not like us to test classes to have interfaces implemented, but we want
15-
# to ensure that classes are not accidentally loose their interfaces.
16-
- '#Call to static method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) with .* will always evaluate to true#'
1714
-
18-
messages:
19-
- '#Parameter \$object of anonymous function has invalid type [A-Z]#'
20-
- '#Anonymous function should return [A-Z] but returns object#'
21-
- '#Anonymous function has invalid return type [A-Z]#'
22-
- '#Instantiated class [A-Z] not found#'
23-
- '#Class [A-Z] not found.#'
24-
path: tests/unit/Container/ContainerConfiguratorTest.php
15+
message: '#Call to static method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) .* will always evaluate to true.#'
16+
path: tests/*

src/Container/ServiceExtensions.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,17 @@ protected function resolveById(string $id, $service, Container $container)
9494
*
9595
* @return mixed
9696
*
97-
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
98-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
97+
* phpcs:disable Generic.Metrics.CyclomaticComplexity
98+
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
9999
*/
100100
protected function resolveByType(
101101
string $className,
102102
object $service,
103103
Container $container,
104104
array $extendedClasses = []
105105
) {
106-
107-
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
108-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
106+
// phpcs:enable Generic.Metrics.CyclomaticComplexity
107+
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
109108

110109
$extendedClasses[] = $className;
111110

src/Module/ExtendingModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Psr\Container\ContainerInterface;
88

99
/**
10-
* @phpstan-type ExtendingService callable(mixed $service, ContainerInterface $container):mixed
10+
* @phpstan-type ExtendingService callable(mixed $service, ContainerInterface $container): mixed
1111
*/
1212
interface ExtendingModule extends Module
1313
{

src/Module/ServiceModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Psr\Container\ContainerInterface;
88

99
/**
10-
* @phpstan-type Service callable(ContainerInterface $container):mixed
10+
* @phpstan-type Service callable(ContainerInterface $container): mixed
1111
*/
1212
interface ServiceModule extends Module
1313
{

src/Package.php

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
/**
1818
* @phpstan-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
1919
* @phpstan-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
20-
*
21-
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
2220
*/
2321
class Package
2422
{

src/Properties/BaseProperties.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Inpsyde\Modularity\Properties;
66

7-
/**
8-
* phpcs:disable PHPCompatibility.FunctionDeclarations.RemovedImplicitlyNullableParam.Deprecated
9-
*/
107
class BaseProperties implements Properties
118
{
129
protected ?bool $isDebug = null;
@@ -25,7 +22,7 @@ class BaseProperties implements Properties
2522
protected function __construct(
2623
string $baseName,
2724
string $basePath,
28-
string $baseUrl = null,
25+
?string $baseUrl = null,
2926
array $properties = []
3027
) {
3128

src/Properties/LibraryProperties.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
/**
88
* @phpstan-type ComposerAuthor array{
99
* name: string,
10-
* email?:string,
11-
* homepage?:string,
12-
* role?:string
10+
* email?: string,
11+
* homepage?: string,
12+
* role?: string,
1313
* }
1414
* @phpstan-type ComposerData array{
1515
* name: string,
16-
* version?:string,
16+
* version?: string,
1717
* require?: array<string, string>,
18-
* require-dev?:array<string, string>,
19-
* description?:string,
20-
* keywords?:string[],
21-
* authors?:ComposerAuthor[],
22-
* extra?:array{modularity?:array<string,string>}
18+
* require-dev?: array<string, string>,
19+
* description?: string,
20+
* keywords?: string[],
21+
* authors?: ComposerAuthor[],
22+
* extra?: array{modularity?: array<string, string>},
2323
* }
2424
*/
2525
class LibraryProperties extends BaseProperties
@@ -124,9 +124,9 @@ public function withBaseUrl(string $url): LibraryProperties
124124
}
125125

126126
/**
127-
* @param array{name?:string} $composerJsonData
127+
* @param ComposerData $composerJsonData
128128
*
129-
* @return list{string, string}
129+
* @return array{string, string}
130130
*/
131131
protected static function buildNames(array $composerJsonData): array
132132
{
@@ -152,19 +152,18 @@ protected static function buildNames(array $composerJsonData): array
152152
* `5.6 || >= 7.1` returns `5.6`
153153
* `>= 7.1 < 8` returns `7.1`
154154
*
155-
* @param array{require?:array<string,string>, require-dev?:array<string,string>} $composerData
155+
* @param ComposerData $composerData
156156
* @param string $key
157157
*
158158
* @return string
159159
*
160160
* phpcs:disable Generic.Metrics.CyclomaticComplexity
161-
* phpcs:disable Syde.Files.LineLengthTooLong
162161
*/
163162
protected static function extractPhpVersion(
164163
array $composerData,
165164
string $key = 'require'
166165
): string {
167-
166+
// phpcs:enable Generic.Metrics.CyclomaticComplexity
168167
$nextKey = ($key === 'require')
169168
? 'require-dev'
170169
: null;

tests/src/TestCase.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,29 @@ protected function stubProperties(
6868
* @param string $id
6969
* @param class-string ...$interfaces
7070
*
71-
* @return Module&MockInterface
71+
* @return Module|MockInterface
7272
*/
73-
protected function stubModule(string $id = 'module', string ...$interfaces): Module
73+
protected function stubModule(string $id = 'module', string ...$interfaces)
7474
{
75-
if (!$interfaces) {
76-
$interfaces[] = Module::class;
77-
}
78-
79-
$stub = \Mockery::mock(...$interfaces);
80-
/** @phpstan-ignore-next-line */
75+
$stub = \Mockery::mock(Module::class, ...$interfaces);
8176
$stub->allows('id')->andReturn($id);
8277

8378
if (in_array(ServiceModule::class, $interfaces, true)) {
84-
/** @phpstan-ignore-next-line */
8579
$stub->allows('services')->byDefault()->andReturn([]);
8680
}
8781

8882
if (in_array(FactoryModule::class, $interfaces, true)) {
89-
/** @phpstan-ignore-next-line */
9083
$stub->allows('factories')->byDefault()->andReturn([]);
9184
}
9285

9386
if (in_array(ExtendingModule::class, $interfaces, true)) {
94-
/** @phpstan-ignore-next-line */
9587
$stub->allows('extensions')->byDefault()->andReturn([]);
9688
}
9789

9890
if (in_array(ExecutableModule::class, $interfaces, true)) {
99-
/** @phpstan-ignore-next-line */
10091
$stub->allows('run')->byDefault()->andReturn(false);
10192
}
10293

103-
/** @var MockInterface&Module */
10494
return $stub;
10595
}
10696

tests/unit/Container/ContainerConfiguratorTest.php

+14-19
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,18 @@ static function (): int {
374374
static::assertSame(0, $container->get('int'));
375375
}
376376

377+
private function loadStubs(): void
378+
{
379+
require_once __DIR__ . '/../stubs.php';
380+
}
381+
377382
/**
378383
* @test
379384
* @runInSeparateProcess
380385
*/
381386
public function testExtensionByTypeNoInfiniteRecursion(): void
382387
{
383-
// We can't declare classes inside a class, but we can eval it.
384-
$php = <<<'PHP'
385-
class A {}
386-
class B extends A {}
387-
PHP;
388-
eval($php);
388+
$this->loadStubs();
389389

390390
$called = [];
391391

@@ -422,14 +422,17 @@ static function () use (&$called): \B {
422422
public function testExtensionByTypeNested(): void
423423
{
424424
// phpcs:enable Inpsyde.CodeQuality.NestingLevel
425+
425426
$logs = [];
426427
/**
427-
* @param object<\ArrayAccess> $object
428+
* @template T
429+
*
430+
* @param object&T $object
428431
* @param int ...$nums
429432
*
430-
* @return object<\ArrayAccess>
433+
* @return object&T
431434
*/
432-
$log = static function (object $object, int ...$nums) use (&$logs): object {
435+
$log = static function ($object, int ...$nums) use (&$logs) {
433436
foreach ($nums as $num) {
434437
if (!in_array($num, $logs, true)) {
435438
$logs[] = $num;
@@ -448,15 +451,7 @@ public function testExtensionByTypeNested(): void
448451
};
449452
$configurator->addService('test', $service);
450453

451-
// We can't declare classes inside a class, but we can eval it.
452-
$php = <<<'PHP'
453-
class A {}
454-
class B extends A {}
455-
class C {}
456-
class D {};
457-
class E extends D {};
458-
PHP;
459-
eval($php);
454+
$this->loadStubs();
460455

461456
$configurator->addExtension(
462457
'@instanceof<D>',
@@ -472,7 +467,7 @@ static function (\A $object) use (&$log): \A {
472467
);
473468
$configurator->addExtension(
474469
'@instanceof<ArrayAccess>',
475-
static function (\ArrayAccess $object) use (&$log): \ArrayAccess {
470+
static function (\ArrayAccess $object) use (&$log): object {
476471
/** @var \ArrayAccess<string, string> */
477472
return $log($object, 2);
478473
}

tests/unit/Container/ReadOnlyContainerTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public function testServiceExtensionsBackwardCompatibility(): void
205205
public function testServiceExtensionsBackwardCompatibilityBreaksOnWrongType(): void
206206
{
207207
$this->expectException(\TypeError::class);
208-
$container = new Container([], [], ServiceExtensions::class, []);
208+
// @phpstan-ignore argument.type (pass invalid type on purpose to trigger exception)
209+
new Container([], [], ServiceExtensions::class, []);
209210
}
210211

211212
/**

tests/unit/Container/ServiceExtensionsTest.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Inpsyde\Modularity\Container\ServiceExtensions;
88
use Inpsyde\Modularity\Tests\TestCase;
99

10+
/**
11+
* @phpstan-type TestObject object{count: integer}&\stdClass
12+
*/
13+
1014
class ServiceExtensionsTest extends TestCase
1115
{
1216
/**
1317
* @test
14-
*
15-
* @phpstan-type TestObject object{ count: integer }&\stdClass
1618
*/
1719
public function testBasicFunctionality(): void
1820
{
@@ -22,7 +24,7 @@ public function testBasicFunctionality(): void
2224
$serviceExtensions->add(
2325
'thing',
2426
static function (object $thing) use (&$expected): object {
25-
/** @var object{count:integer}&\stdClass $thing */
27+
/** @var TestObject $thing */
2628
$thing->count++;
2729
$expected++;
2830

@@ -33,7 +35,7 @@ static function (object $thing) use (&$expected): object {
3335
$serviceExtensions->add(
3436
'nothing',
3537
static function (object $thing): object {
36-
/** @var object{count:integer}&\stdClass $thing */
38+
/** @var TestObject $thing */
3739
$thing->count++;
3840

3941
return $thing;
@@ -43,7 +45,7 @@ static function (object $thing): object {
4345
$serviceExtensions->add(
4446
'thing',
4547
static function (object $thing) use (&$expected): object {
46-
/** @var object{count:integer}&\stdClass $thing */
48+
/** @var TestObject $thing */
4749
$thing->count++;
4850
$expected++;
4951

@@ -54,7 +56,7 @@ static function (object $thing) use (&$expected): object {
5456
$serviceExtensions->add(
5557
ServiceExtensions::typeId(\stdClass::class),
5658
static function (object $thing) use (&$expected): object {
57-
/** @var object{count:integer}&\stdClass $thing */
59+
/** @var TestObject $thing */
5860
$thing->count++;
5961
$expected++;
6062

@@ -65,7 +67,7 @@ static function (object $thing) use (&$expected): object {
6567
$serviceExtensions->add(
6668
ServiceExtensions::typeId(\ArrayObject::class),
6769
static function (object $thing): object {
68-
/** @var object{count:integer}&\stdClass $thing */
70+
/** @var TestObject $thing */
6971
$thing->count++;
7072

7173
return $thing;

0 commit comments

Comments
 (0)