Skip to content

Commit

Permalink
dev: Tools update (PHPStan v1.11.4) (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru authored Jun 8, 2024
2 parents d84265b + c4826f8 commit 31279fa
Show file tree
Hide file tree
Showing 21 changed files with 1,888 additions and 1,505 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
"jq --indent 2 '.' composer.json > composer.tmp && mv composer.tmp composer.json"
],
"phpstan": [
"./vendor-bin/phpstan/vendor/bin/phpstan analyse"
"./vendor-bin/phpstan/vendor/bin/phpstan analyse -v"
],
"phpunit": [
"./vendor/bin/phpunit"
Expand Down
6 changes: 5 additions & 1 deletion packages/dev/src/PhpStan/ClassMustBeFinal/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LastDragon_ru\LaraASP\Dev\PhpStan\ClassMustBeFinal;

use Illuminate\Support\Str;
use LastDragon_ru\LaraASP\Dev\Package;
use Override;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -60,7 +62,9 @@ public function processNode(Node $node, Scope $scope): array {
return [
RuleErrorBuilder::message(
sprintf('Class `%s` must be `final`.', $reflection->getName()),
)->build(),
)
->identifier(Str::camel(Package::Name).'.classMustBeFinal')
->build(),
];
}

Expand Down
6 changes: 5 additions & 1 deletion packages/dev/src/PhpStan/ClassMustBeInternal/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LastDragon_ru\LaraASP\Dev\PhpStan\ClassMustBeInternal;

use Illuminate\Support\Str;
use LastDragon_ru\LaraASP\Dev\Package;
use Override;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -74,7 +76,9 @@ public function processNode(Node $node, Scope $scope): array {
return [
RuleErrorBuilder::message(
sprintf('Class `%s` must be marked by `@internal`.', $reflection->getName()),
)->build(),
)
->identifier(Str::camel(Package::Name).'.classMustBeInternal')
->build(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// @phpcs:disable Generic.Files.LineLength.TooLong

use ArrayAccess;
use GraphQL\Language\Parser;
use LastDragon_ru\LaraASP\Core\Utils\Path;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Context;
Expand Down Expand Up @@ -64,8 +65,12 @@ public function testInvoke(): void {
}

public function testInvokeNoPrinter(): void {
unset($this->app()[PrinterContract::class]);
// Reset
if ($this->app() instanceof ArrayAccess) {
unset($this->app()[PrinterContract::class]);
}

// Test
$root = new Directory(Path::normalize(__DIR__), false);
$file = new File(Path::normalize(__FILE__), false);
$target = '@test';
Expand Down
2 changes: 1 addition & 1 deletion packages/documentator/src/Preprocessor/Preprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getInstructions(): array {
* @param I|class-string<I> $instruction
*/
public function addInstruction(Instruction|string $instruction): static {
// @phpstan-ignore-next-line Assigment is fine...
// @phpstan-ignore-next-line argument.type (Assigment is fine...)
$this->instructions[$instruction::getName()] = new ResolvedInstruction($this->container, $instruction);

return $this;
Expand Down
6 changes: 4 additions & 2 deletions packages/eloquent/src/Concerns/SaveOrThrow.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public function save(array $options = []): bool {
return $result;
}

/* @phpstan-ignore-next-line `Model::delete()` and `Pivot::::delete()` return different types. */
/**
* @phpstan-ignore-next-line method.childReturnType (`Model::delete()`&`Pivot::::delete()` return different types)
*/
#[Override]
public function delete(): bool|int|null {
$result = parent::delete();

if ($result === false) { // @phpstan-ignore-line
if ($result === false) { // @phpstan-ignore-line method.childReturnType, identical.alwaysFalse
throw new Exception('An unknown error occurred while deleting the model.');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/eloquent/src/ModelHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ModelHelperTest__Model extends Model {
/**
* @noinspection PhpMissingReturnTypeInspection
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
* @phpstan-ignore-next-line Required for test
* @phpstan-ignore-next-line missingType.return (Required for test)
*/
public function noTypeHint() {
return $this->belongsTo(self::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ static function (NamedTypeNode|ObjectType $type, string $operation) use ($defaul
$name = $type instanceof NamedTypeNode
? $type->name->value
: $type->name;
$custom = !isset($default[$operation])
|| $default[$operation] !== $name;
$custom = $default[$operation] !== $name;

return $custom;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/Stream/Directives/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ protected function getResolverQuery(string $type, string $field): ?array {
$value = new class($type, $field) extends FieldValue {
/**
* @noinspection PhpMissingParentConstructorInspection
* @phpstan-ignore-next-line no need to call parent `__construct`
* @phpstan-ignore-next-line constructor.missingParentCall (no need to call parent `__construct`)
*/
public function __construct(
private readonly string $typeName,
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/Testing/SchemaBuilderWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SchemaBuilderWrapper extends SchemaBuilder {

/**
* @noinspection PhpMissingParentConstructorInspection
* @phpstan-ignore-next-line no need to call parent `__construct`
* @phpstan-ignore-next-line constructor.missingParentCall (no need to call parent `__construct`)
*/
public function __construct(
protected SchemaBuilder $builder,
Expand Down
20 changes: 14 additions & 6 deletions packages/serializer/src/Metadata/MetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ final class MetadataFactoryTest extends TestCase {
public function testHasMetadataFor(): void {
$factory = new MetadataFactory();

self::assertTrue($factory->hasMetadataFor(MetadataFactoryTest_A::class)); // @phpstan-ignore-line
self::assertTrue($factory->hasMetadataFor(new MetadataFactoryTest_A())); // @phpstan-ignore-line
self::assertFalse($factory->hasMetadataFor(JsonSerializable::class)); // @phpstan-ignore-line
self::assertFalse($factory->hasMetadataFor('UnknownClass')); // @phpstan-ignore-line
self::assertTrue(
$factory->hasMetadataFor(MetadataFactoryTest_A::class), // @phpstan-ignore-line method.alreadyNarrowedType
);
self::assertTrue(
$factory->hasMetadataFor(new MetadataFactoryTest_A()), // @phpstan-ignore-line method.alreadyNarrowedType
);
self::assertFalse(
$factory->hasMetadataFor(JsonSerializable::class), // @phpstan-ignore-line method.alreadyNarrowedType
);
self::assertFalse(
$factory->hasMetadataFor('UnknownClass'), // @phpstan-ignore-line method.impossibleType
);
}

public function testGetMetadataFor(): void {
Expand Down Expand Up @@ -144,7 +152,7 @@ class : MetadataFactoryTest_B::class,
#[DiscriminatorMap('version', ['b' => MetadataFactoryTest_B::class, 'a' => MetadataFactoryTest_A::class])]
class MetadataFactoryTest_A implements JsonSerializable {
public int $a = 123;
public bool $b; // @phpstan-ignore-line required for tests
public bool $b; // @phpstan-ignore-line property.uninitialized (required for tests)
protected string $c = 'should be ignored';
private string $d = 'should be ignored';
public static string $e = 'should be ignored';
Expand All @@ -162,7 +170,7 @@ public function jsonSerialize(): mixed {
class MetadataFactoryTest_B extends MetadataFactoryTest_A {
/**
* @var array<array-key, MetadataFactoryTest_A>
* @phpstan-ignore-next-line required for tests
* @phpstan-ignore-next-line property.uninitialized (required for tests)
*/
public array $array;

Expand Down
6 changes: 3 additions & 3 deletions packages/serializer/src/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function __construct(
$backedEnum = ProviderTest__BackedEnum::A;
$serializable = new ProviderTest__Simple($datetime, $unitEnum, $backedEnum);
$curcular = new class() implements Serializable {
public Serializable $a; // @phpstan-ignore-line required for tests
public Serializable $a; // @phpstan-ignore-line property.uninitialized (required for tests)
};
$curcular->a = $curcular;

Expand Down Expand Up @@ -374,7 +374,7 @@ public function __construct(
*/
class ProviderTest__Simple implements Serializable, Stringable, JsonSerializable {
public int $a = 123;
public bool $b; // @phpstan-ignore-line required for tests
public bool $b; // @phpstan-ignore-line property.uninitialized (required for tests)
protected string $c = 'should be ignored';
private string $d = 'should be ignored';
public ?DateTimeInterface $e = null;
Expand Down Expand Up @@ -411,7 +411,7 @@ public function jsonSerialize(): mixed {
*/
class ProviderTest__Complex implements Serializable {
public int $a = 123;
public bool $b; // @phpstan-ignore-line required for tests
public bool $b; // @phpstan-ignore-line property.uninitialized (required for tests)

/**
* @var array<int, int>
Expand Down
1 change: 1 addition & 0 deletions packages/spa/src/Http/Resources/ResourceCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testConstruct(bool|Exception $expected, string $class): void {
self::expectExceptionObject($expected);
}

// @phpstan-ignore-next-line expr.resultUnused
new class($class, []) extends ResourceCollection {
// empty
};
Expand Down
1 change: 1 addition & 0 deletions packages/spa/src/Http/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testConstruct(bool|Exception $expected, mixed $value): void {
self::expectExceptionObject($expected);
}

// @phpstan-ignore-next-line expr.resultUnused
new class($value) extends Resource {
// empty
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function normalize(Query $query): Query {
$matches = array_flip($matches);

uksort($matches, static function (string|int $a, string|int $b): int {
return strlen((string) $b) <=> strlen((string) $a);
return strlen("{$b}") <=> strlen("{$a}");
});

foreach ($matches as $match => $index) {
Expand Down
3 changes: 1 addition & 2 deletions packages/testing/src/Concerns/Override.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ protected function override(string $class, mixed $factory = null): mixed {
: $factory;

if (is_callable($factory)) {
/** @phpstan-ignore-next-line it may return `void` so it is fine here */
$mock = $factory($mock, $this) ?: $mock;
} elseif (is_string($factory)) {
$mock = $this->app()->make($factory);
Expand All @@ -112,6 +111,6 @@ protected function override(string $class, mixed $factory = null): mixed {
);

// Return
return $mock; // @phpstan-ignore-line `ContainerExtension` is not so smart yet.
return $mock; // @phpstan-ignore-line return.type (`ContainerExtension` is not so smart yet).
}
}
Loading

0 comments on commit 31279fa

Please sign in to comment.