Skip to content

Commit

Permalink
address PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrommen committed Jan 28, 2025
1 parent 81f58ff commit d5064db
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 91 deletions.
19 changes: 11 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
* text eol=lf

tests export-ignore
.github export-ignore
.gitattributes export-ignore
.gitignore export-ignore
README.md export-ignore
phpcs.xml.dist export-ignore
phpunit.xml.dist export-ignore
phpstan.neon.dist export-ignore
*.php diff=php

/.github/ export-ignore
/docs/ export-ignore
/tests/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/README.md export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ruleset>
<file>./src</file>
<file>./tests</file>
<exclude-pattern>./tests/unit/stubs.php</exclude-pattern>

<arg value="sp"/>
<arg name="colors"/>
Expand Down
16 changes: 4 additions & 12 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-mockery/extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/szepeviktor/phpstan-wordpress/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/swissspidy/phpstan-no-private/rules.neon
- vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
level: 8
paths:
- src/
- tests/
treatPhpDocTypesAsCertain: false
ignoreErrors:
# It seems like PHPStan does not like us to test classes to have interfaces implemented, but we want
# to ensure that classes are not accidentally loose their interfaces.
- '#Call to static method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) with .* will always evaluate to true#'
-
messages:
- '#Parameter \$object of anonymous function has invalid type [A-Z]#'
- '#Anonymous function should return [A-Z] but returns object#'
- '#Anonymous function has invalid return type [A-Z]#'
- '#Instantiated class [A-Z] not found#'
- '#Class [A-Z] not found.#'
path: tests/unit/Container/ContainerConfiguratorTest.php
message: '#Call to static method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) .* will always evaluate to true.#'
path: tests/*
9 changes: 4 additions & 5 deletions src/Container/ServiceExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,17 @@ protected function resolveById(string $id, $service, Container $container)
*
* @return mixed
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
* phpcs:disable Generic.Metrics.CyclomaticComplexity
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
*/
protected function resolveByType(
string $className,
object $service,
Container $container,
array $extendedClasses = []
) {

// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
// phpcs:enable Generic.Metrics.CyclomaticComplexity
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration

$extendedClasses[] = $className;

Expand Down
2 changes: 1 addition & 1 deletion src/Module/ExtendingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Container\ContainerInterface;

/**
* @phpstan-type ExtendingService callable(mixed $service, ContainerInterface $container):mixed
* @phpstan-type ExtendingService callable(mixed $service, ContainerInterface $container): mixed
*/
interface ExtendingModule extends Module
{
Expand Down
2 changes: 1 addition & 1 deletion src/Module/ServiceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Container\ContainerInterface;

/**
* @phpstan-type Service callable(ContainerInterface $container):mixed
* @phpstan-type Service callable(ContainerInterface $container): mixed
*/
interface ServiceModule extends Module
{
Expand Down
2 changes: 0 additions & 2 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
/**
* @phpstan-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
* @phpstan-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
*
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
*/
class Package
{
Expand Down
5 changes: 1 addition & 4 deletions src/Properties/BaseProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Inpsyde\Modularity\Properties;

/**
* phpcs:disable PHPCompatibility.FunctionDeclarations.RemovedImplicitlyNullableParam.Deprecated
*/
class BaseProperties implements Properties
{
protected ?bool $isDebug = null;
Expand All @@ -25,7 +22,7 @@ class BaseProperties implements Properties
protected function __construct(
string $baseName,
string $basePath,
string $baseUrl = null,
?string $baseUrl = null,
array $properties = []
) {

Expand Down
27 changes: 13 additions & 14 deletions src/Properties/LibraryProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
/**
* @phpstan-type ComposerAuthor array{
* name: string,
* email?:string,
* homepage?:string,
* role?:string
* email?: string,
* homepage?: string,
* role?: string,
* }
* @phpstan-type ComposerData array{
* name: string,
* version?:string,
* version?: string,
* require?: array<string, string>,
* require-dev?:array<string, string>,
* description?:string,
* keywords?:string[],
* authors?:ComposerAuthor[],
* extra?:array{modularity?:array<string,string>}
* require-dev?: array<string, string>,
* description?: string,
* keywords?: string[],
* authors?: ComposerAuthor[],
* extra?: array{modularity?: array<string, string>},
* }
*/
class LibraryProperties extends BaseProperties
Expand Down Expand Up @@ -124,9 +124,9 @@ public function withBaseUrl(string $url): LibraryProperties
}

/**
* @param array{name?:string} $composerJsonData
* @param ComposerData $composerJsonData
*
* @return list{string, string}
* @return array{string, string}
*/
protected static function buildNames(array $composerJsonData): array
{
Expand All @@ -152,19 +152,18 @@ protected static function buildNames(array $composerJsonData): array
* `5.6 || >= 7.1` returns `5.6`
* `>= 7.1 < 8` returns `7.1`
*
* @param array{require?:array<string,string>, require-dev?:array<string,string>} $composerData
* @param ComposerData $composerData
* @param string $key
*
* @return string
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity
* phpcs:disable Syde.Files.LineLengthTooLong
*/
protected static function extractPhpVersion(
array $composerData,
string $key = 'require'
): string {

// phpcs:enable Generic.Metrics.CyclomaticComplexity
$nextKey = ($key === 'require')
? 'require-dev'
: null;
Expand Down
16 changes: 3 additions & 13 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,29 @@ protected function stubProperties(
* @param string $id
* @param class-string ...$interfaces
*
* @return Module&MockInterface
* @return Module|MockInterface
*/
protected function stubModule(string $id = 'module', string ...$interfaces): Module
protected function stubModule(string $id = 'module', string ...$interfaces)
{
if (!$interfaces) {
$interfaces[] = Module::class;
}

$stub = \Mockery::mock(...$interfaces);
/** @phpstan-ignore-next-line */
$stub = \Mockery::mock(Module::class, ...$interfaces);
$stub->allows('id')->andReturn($id);

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

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

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

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

/** @var MockInterface&Module */
return $stub;
}

Expand Down
33 changes: 14 additions & 19 deletions tests/unit/Container/ContainerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,18 @@ static function (): int {
static::assertSame(0, $container->get('int'));
}

private function loadStubs(): void
{
require_once __DIR__ . '/../stubs.php';
}

/**
* @test
* @runInSeparateProcess
*/
public function testExtensionByTypeNoInfiniteRecursion(): void
{
// We can't declare classes inside a class, but we can eval it.
$php = <<<'PHP'
class A {}
class B extends A {}
PHP;
eval($php);
$this->loadStubs();

$called = [];

Expand Down Expand Up @@ -422,14 +422,17 @@ static function () use (&$called): \B {
public function testExtensionByTypeNested(): void
{
// phpcs:enable Inpsyde.CodeQuality.NestingLevel

$logs = [];
/**
* @param object<\ArrayAccess> $object
* @template T
*
* @param object&T $object
* @param int ...$nums
*
* @return object<\ArrayAccess>
* @return object&T
*/
$log = static function (object $object, int ...$nums) use (&$logs): object {
$log = static function ($object, int ...$nums) use (&$logs) {
foreach ($nums as $num) {
if (!in_array($num, $logs, true)) {
$logs[] = $num;
Expand All @@ -448,15 +451,7 @@ public function testExtensionByTypeNested(): void
};
$configurator->addService('test', $service);

// We can't declare classes inside a class, but we can eval it.
$php = <<<'PHP'
class A {}
class B extends A {}
class C {}
class D {};
class E extends D {};
PHP;
eval($php);
$this->loadStubs();

$configurator->addExtension(
'@instanceof<D>',
Expand All @@ -472,7 +467,7 @@ static function (\A $object) use (&$log): \A {
);
$configurator->addExtension(
'@instanceof<ArrayAccess>',
static function (\ArrayAccess $object) use (&$log): \ArrayAccess {
static function (\ArrayAccess $object) use (&$log): object {
/** @var \ArrayAccess<string, string> */
return $log($object, 2);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Container/ReadOnlyContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ public function testServiceExtensionsBackwardCompatibility(): void
public function testServiceExtensionsBackwardCompatibilityBreaksOnWrongType(): void
{
$this->expectException(\TypeError::class);
$container = new Container([], [], ServiceExtensions::class, []);
// @phpstan-ignore argument.type (pass invalid type on purpose to trigger exception)
new Container([], [], ServiceExtensions::class, []);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/Container/ServiceExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Inpsyde\Modularity\Container\ServiceExtensions;
use Inpsyde\Modularity\Tests\TestCase;

/**
* @phpstan-type TestObject object{count: integer}&\stdClass
*/

class ServiceExtensionsTest extends TestCase
{
/**
* @test
*
* @phpstan-type TestObject object{ count: integer }&\stdClass
*/
public function testBasicFunctionality(): void
{
Expand All @@ -22,7 +24,7 @@ public function testBasicFunctionality(): void
$serviceExtensions->add(
'thing',
static function (object $thing) use (&$expected): object {
/** @var object{count:integer}&\stdClass $thing */
/** @var TestObject $thing */
$thing->count++;
$expected++;

Expand All @@ -33,7 +35,7 @@ static function (object $thing) use (&$expected): object {
$serviceExtensions->add(
'nothing',
static function (object $thing): object {
/** @var object{count:integer}&\stdClass $thing */
/** @var TestObject $thing */
$thing->count++;

return $thing;
Expand All @@ -43,7 +45,7 @@ static function (object $thing): object {
$serviceExtensions->add(
'thing',
static function (object $thing) use (&$expected): object {
/** @var object{count:integer}&\stdClass $thing */
/** @var TestObject $thing */
$thing->count++;
$expected++;

Expand All @@ -54,7 +56,7 @@ static function (object $thing) use (&$expected): object {
$serviceExtensions->add(
ServiceExtensions::typeId(\stdClass::class),
static function (object $thing) use (&$expected): object {
/** @var object{count:integer}&\stdClass $thing */
/** @var TestObject $thing */
$thing->count++;
$expected++;

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

return $thing;
Expand Down
Loading

0 comments on commit d5064db

Please sign in to comment.