Skip to content
Open

Psalm 7 #1600

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
495 changes: 340 additions & 155 deletions psalm-baseline.xml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@
<issueHandlers>
<ClassMustBeFinal errorLevel="suppress"/>
<MissingOverrideAttribute errorLevel="suppress"/>
<!-- Reported only on PHP < 8.6 runtime: inherited class-string<T> of non-generic ReflectionEnum/ReflectionObject -->
<LessSpecificImplementedReturnType>
<errorLevel type="suppress">
<file name="src/Reflection/Adapter/ReflectionEnum.php"/>
<file name="src/Reflection/Adapter/ReflectionObject.php"/>
</errorLevel>
</LessSpecificImplementedReturnType>
</issueHandlers>
<taintAnalysis>
<ignoreFiles>
<directory name="src" />
</ignoreFiles>
</taintAnalysis>
</psalm>
1 change: 0 additions & 1 deletion src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ public function hasConstant(string $name): bool
*/
public function getConstants(int|null $filter = null): array
{
/** @psalm-suppress ImpureFunctionCall */
return array_map(
fn (BetterReflectionClassConstant|BetterReflectionEnumCase $betterConstantOrEnumCase): mixed => $this->getConstantValue($betterConstantOrEnumCase),
$this->filterBetterReflectionClassConstants($filter),
Expand Down
13 changes: 7 additions & 6 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function __get(string $name): mixed
throw new OutOfBoundsException(sprintf('Property %s::$%s does not exist.', self::class, $name));
}

/** @return class-string<UnitEnum> */
public function getName(): string
{
/** @phpstan-var class-string<UnitEnum> */
Expand Down Expand Up @@ -115,16 +116,19 @@ public function getFileName(): string|false
return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/** @return positive-int */
public function getStartLine(): int
{
return $this->betterReflectionEnum->getStartLine();
}

/** @return positive-int */
public function getEndLine(): int
{
return $this->betterReflectionEnum->getEndLine();
}

/** @return non-empty-string|false */
public function getDocComment(): string|false
{
return $this->betterReflectionEnum->getDocComment() ?? false;
Expand Down Expand Up @@ -225,7 +229,6 @@ public function hasConstant(string $name): bool
*/
public function getConstants(int|null $filter = null): array
{
/** @psalm-suppress ImpureFunctionCall */
return array_map(
fn (BetterReflectionClassConstant|BetterReflectionEnumCase $betterConstantOrEnumCase): mixed => $this->getConstantValue($betterConstantOrEnumCase),
$this->filterBetterReflectionClassConstants($filter),
Expand Down Expand Up @@ -375,6 +378,7 @@ public function isReadOnly(): bool
return $this->betterReflectionEnum->isReadOnly();
}

/** @return int-mask-of<ReflectionClass::IS_*> */
public function getModifiers(): int
{
return $this->betterReflectionEnum->getModifiers();
Expand Down Expand Up @@ -482,11 +486,7 @@ public function isSubclassOf(CoreReflectionClass|string $class): bool
return $this->implementsInterface($className);
}

/**
* @return array<string, mixed>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
/** @return array<string, mixed> */
public function getStaticProperties(): array
{
return $this->betterReflectionEnum->getStaticProperties();
Expand Down Expand Up @@ -554,6 +554,7 @@ public function getNamespaceName(): string
return $this->betterReflectionEnum->getNamespaceName() ?? '';
}

/** @return non-empty-string */
public function getShortName(): string
{
return $this->betterReflectionEnum->getShortName();
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Adapter/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ public function hasPrototype(): bool
/**
* @codeCoverageIgnore
* @infection-ignore-all
* @psalm-pure
* @phpstan-ignore pureMethod.void
* @psalm-mutation-free
*/
public function setAccessible(bool $accessible): void
{
Expand Down
42 changes: 31 additions & 11 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function __toString(): string
return $this->betterReflectionObject->__toString();
}

/** @psalm-mutation-free */
/**
* @return class-string
*
* @psalm-mutation-free
*/
public function getName(): string
{
return $this->betterReflectionObject->getName();
Expand Down Expand Up @@ -82,19 +86,31 @@ public function getFileName(): string|false
return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/** @psalm-mutation-free */
/**
* @return positive-int
*
* @psalm-mutation-free
*/
public function getStartLine(): int
{
return $this->betterReflectionObject->getStartLine();
}

/** @psalm-mutation-free */
/**
* @return positive-int
*
* @psalm-mutation-free
*/
public function getEndLine(): int
{
return $this->betterReflectionObject->getEndLine();
}

/** @psalm-mutation-free */
/**
* @return non-empty-string|false
*
* @psalm-mutation-free
*/
public function getDocComment(): string|false
{
return $this->betterReflectionObject->getDocComment() ?? false;
Expand Down Expand Up @@ -370,7 +386,11 @@ public function isReadOnly(): bool
return $this->betterReflectionObject->isReadOnly();
}

/** @psalm-mutation-free */
/**
* @return int-mask-of<ReflectionClass::IS_*>
*
* @psalm-mutation-free
*/
public function getModifiers(): int
{
return $this->betterReflectionObject->getModifiers();
Expand Down Expand Up @@ -483,11 +503,7 @@ public function isSubclassOf(CoreReflectionClass|string $class): bool
return $this->betterReflectionObject->isSubclassOf($realParentClassName);
}

/**
* @return array<string, mixed>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
/** @return array<string, mixed> */
public function getStaticProperties(): array
{
return $this->betterReflectionObject->getStaticProperties();
Expand Down Expand Up @@ -600,7 +616,11 @@ public function getNamespaceName(): string
return $this->betterReflectionObject->getNamespaceName() ?? '';
}

/** @psalm-mutation-free */
/**
* @return non-empty-string
*
* @psalm-mutation-free
*/
public function getShortName(): string
{
return $this->betterReflectionObject->getShortName();
Expand Down
6 changes: 5 additions & 1 deletion src/Reflection/Adapter/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ValueError;

use function array_map;
use function assert;
use function count;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -91,7 +92,7 @@
}

if ($type instanceof BetterReflectionIntersectionType) {
return null;

Check warning on line 95 in src/Reflection/Adapter/ReflectionParameter.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ } if ($type instanceof BetterReflectionIntersectionType) { - return null; + } if ($type instanceof BetterReflectionNamedType) {
}

if ($type instanceof BetterReflectionNamedType) {
Expand All @@ -100,13 +101,15 @@
$unionTypes = $type->getTypes();

if (count($unionTypes) !== 2) {
return null;

Check warning on line 104 in src/Reflection/Adapter/ReflectionParameter.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ $unionTypes = $type->getTypes(); if (count($unionTypes) !== 2) { - return null; + } if (! $type->allowsNull()) {
}

if (! $type->allowsNull()) {
return null;
}

$classType = null;

foreach ($unionTypes as $unionInnerType) {
if (! $unionInnerType instanceof BetterReflectionNamedType) {
return null;
Expand All @@ -117,12 +120,13 @@
}

$classType = $unionInnerType;
break;

Check warning on line 123 in src/Reflection/Adapter/ReflectionParameter.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "Break_": @@ @@ } $classType = $unionInnerType; - break; + continue; } assert($classType !== null);
}

assert($classType !== null);
}

try {
/** @phpstan-ignore variable.undefined */
return new ReflectionClass($classType->getClass());
} catch (LogicException) {
return null;
Expand All @@ -149,7 +153,7 @@
}

if ($typeReflection instanceof BetterReflectionIntersectionType) {
return false;

Check warning on line 156 in src/Reflection/Adapter/ReflectionParameter.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ } if ($typeReflection instanceof BetterReflectionIntersectionType) { - return false; + } $isOneOfAllowedTypes = static function (BetterReflectionType $namedType, string ...$types): bool {
}

$isOneOfAllowedTypes = static function (BetterReflectionType $namedType, string ...$types): bool {
Expand Down
5 changes: 3 additions & 2 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@
$this->attributes = ReflectionAttributeHelper::createAttributes($reflector, $this, $node->attrGroups);

$startLine = $node->getStartLine();
assert($startLine > 0);

Check warning on line 188 in src/Reflection/ReflectionClass.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "GreaterThan": @@ @@ $this->attributes = ReflectionAttributeHelper::createAttributes($reflector, $this, $node->attrGroups); $startLine = $node->getStartLine(); - assert($startLine > 0); + assert($startLine >= 0); $endLine = $node->getEndLine(); assert($endLine > 0);
$endLine = $node->getEndLine();
assert($endLine > 0);

Check warning on line 190 in src/Reflection/ReflectionClass.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "GreaterThan": @@ @@ $startLine = $node->getStartLine(); assert($startLine > 0); $endLine = $node->getEndLine(); - assert($endLine > 0); + assert($endLine >= 0); $this->startLine = $startLine; $this->endLine = $endLine;

$this->startLine = $startLine;
$this->endLine = $endLine;
Expand Down Expand Up @@ -287,6 +287,7 @@
$fileName = $this->getFileName();

if ($fileName === null) {
/** @psalm-suppress WeakHash */
$fileName = sha1($this->locatedSource->getSource());
}

Expand Down Expand Up @@ -403,7 +404,7 @@
foreach ($traitAliasDefinitions as $traitAliasDefinition) {
if ($lowerCasedMethodHash === $traitAliasDefinition['hash']) {
$modifiersUsedWithAlias = true;
break;

Check warning on line 407 in src/Reflection/ReflectionClass.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "Break_": @@ @@ foreach ($traitAliasDefinitions as $traitAliasDefinition) { if ($lowerCasedMethodHash === $traitAliasDefinition['hash']) { $modifiersUsedWithAlias = true; - break; + continue; } } }
}
}
}
Expand Down Expand Up @@ -946,11 +947,11 @@

if ($node instanceof InterfaceNode) {
$interfaceName = $this->getName();
if ($interfaceName === 'UnitEnum') {
if ($interfaceName === UnitEnum::class) {
$properties['name'] = $createProperty('name', new Node\Identifier('string'));
}

if ($interfaceName === 'BackedEnum') {
if ($interfaceName === BackedEnum::class) {
$properties['value'] = $createProperty('value', new Node\UnionType([
new Node\Identifier('int'),
new Node\Identifier('string'),
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/ReflectionClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ public function isPublic(): bool
*/
public function isPrivate(): bool
{
// Private constant cannot be final
return $this->modifiers === CoreReflectionClassConstant::IS_PRIVATE;
return (bool) ($this->modifiers & CoreReflectionClassConstant::IS_PRIVATE);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ private static function getTrailingDocComment(ParamNode $node, string $source):
return null;
}

/** @psalm-suppress ImpureMethodCall */
$doc = new Doc($docComment);
/** @psalm-suppress ImpureMethodCall */
$reformattedDocComment = $doc->getReformattedText();
Expand Down
2 changes: 0 additions & 2 deletions src/Reflection/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,6 @@ private function getEffectivePropertyAndObjectClass(object $object): array
return [$this, $propertyClass];
}

/** @psalm-suppress ImpureMethodCall */
$objectClass = ReflectionClass::createFromInstance($object);

if ($this->isPrivate()) {
Expand Down Expand Up @@ -765,7 +764,6 @@ private function isDynamicPropertyWritable(object|null $object): bool
if ($object === null) {
$class = $this->getImplementingClass();
} else {
/** @psalm-suppress ImpureMethodCall */
$class = ReflectionClass::createFromInstance($object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ private function getClassNodeData(string $className): array|null
if (! array_key_exists($lowercaseClassName, $this->classNodes)) {
$this->parseFile($filePath);

/** @psalm-suppress RedundantCondition */
if (! array_key_exists($lowercaseClassName, $this->classNodes)) {
// Save `null` so we don't parse the file again for the same $lowercaseClassName
$this->classNodes[$lowercaseClassName] = null;
Expand All @@ -295,7 +294,6 @@ public function generateFunctionStub(string $functionName): StubData|null
if (! array_key_exists($lowercaseFunctionName, $this->functionNodes)) {
$this->parseFile($filePath);

/** @psalm-suppress RedundantCondition */
if (! array_key_exists($lowercaseFunctionName, $this->functionNodes)) {
// Save `null` so we don't parse the file again for the same $lowercaseFunctionName
$this->functionNodes[$lowercaseFunctionName] = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static function (string $file) use ($astLocator): array {

return [new SingleFileSourceLocator($file, $astLocator)];
},
array_merge($classMapFiles, $filePaths),
array_values(array_merge($classMapFiles, $filePaths)),
),
));
}
Expand Down
5 changes: 4 additions & 1 deletion src/SourceLocator/Type/FileIteratorSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class FileIteratorSourceLocator implements SourceLocator
public function __construct(Iterator $fileInfoIterator, private Locator $astLocator)
{
foreach ($fileInfoIterator as $fileInfo) {
/** @phpstan-ignore instanceof.alwaysTrue */
/**
* @psalm-suppress DocblockTypeContradiction
* @phpstan-ignore instanceof.alwaysTrue
*/
if (! $fileInfo instanceof SplFileInfo) {
throw InvalidFileInfo::fromNonSplFileInfo($fileInfo);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"infection/infection": "^0.35.12",
"phpstan/phpstan": "^2.3.7",
"phpstan/phpstan-phpunit": "^2.1.2",
"vimeo/psalm": "^6.21.2",
"vimeo/psalm": "^7.15.0",
"roave/backward-compatibility-check": "^8.27.0"
},
"config": {
Expand Down
Loading
Loading