Skip to content

Commit

Permalink
More psalm to fix the errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Nov 3, 2024
1 parent b84e4b8 commit 3a1eb2e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
10 changes: 3 additions & 7 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public function __construct(public string $name, NamingStrategy|null $namingStra
* Gets the ReflectionProperties of the mapped class.
*
* @return LegacyReflectionFields|ReflectionProperty[] An array of ReflectionProperty instances.
* @psalm-return LegacyReflectionFields|array<ReflectionProperty>
* @psalm-return LegacyReflectionFields|array<string, ReflectionProperty>

Check failure on line 573 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

MismatchingDocblockReturnType

src/Mapping/ClassMetadata.php:573:22: MismatchingDocblockReturnType: Docblock has incorrect return type 'Doctrine\ORM\Mapping\LegacyReflectionFields|array<string, ReflectionProperty>', should be 'array<array-key, mixed>' (see https://psalm.dev/142)

Check failure on line 573 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

MismatchingDocblockReturnType

src/Mapping/ClassMetadata.php:573:22: MismatchingDocblockReturnType: Docblock has incorrect return type 'Doctrine\ORM\Mapping\LegacyReflectionFields|array<string, ReflectionProperty>', should be 'array<array-key, mixed>' (see https://psalm.dev/142)
*/
public function getReflectionProperties(): array

Check failure on line 575 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)

PHPDoc tag @return with type array<string, ReflectionProperty>|Doctrine\ORM\Mapping\LegacyReflectionFields is not subtype of native type array.

Check failure on line 575 in src/Mapping/ClassMetadata.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

PHPDoc tag @return with type array<string, ReflectionProperty>|Doctrine\ORM\Mapping\LegacyReflectionFields is not subtype of native type array.
{
Expand Down Expand Up @@ -600,9 +600,7 @@ public function getPropertyAccessor(string $name): PropertyAccessor|null
return $this->propertyAccessors[$name] ?? null;
}

/**
* @throws BadMethodCallException If the class has a composite identifier.
*/
/** @throws BadMethodCallException If the class has a composite identifier. */
public function getSingleIdReflectionProperty(): ReflectionProperty|null
{
if ($this->isIdentifierComposite) {
Expand All @@ -612,9 +610,7 @@ public function getSingleIdReflectionProperty(): ReflectionProperty|null
return $this->reflFields[$this->identifier[0]];
}

/**
* @throws BadMethodCallException If the class has a composite identifier.
*/
/** @throws BadMethodCallException If the class has a composite identifier. */
public function getSingleIdPropertyAccessor(): PropertyAccessor|null
{
if ($this->isIdentifierComposite) {
Expand Down
40 changes: 27 additions & 13 deletions src/Mapping/LegacyReflectionFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(private ClassMetadata $classMetadata, private Reflec
}

/** @param string $offset */
public function offsetExists($offset): bool
public function offsetExists($offset): bool // phpcs:ignore
{
Deprecation::trigger(
'doctrine/orm',
Expand All @@ -43,8 +43,12 @@ public function offsetExists($offset): bool
return isset($this->classMetadata->propertyAccessors[$offset]);
}

/** @param string $field */
public function offsetGet($field): mixed
/**
* @param string $field
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public function offsetGet($field): mixed // phpcs:ignore
{
if (isset($this->reflFields[$field])) {
return $this->reflFields[$field];
Expand All @@ -60,6 +64,8 @@ public function offsetGet($field): mixed
$fieldName = str_contains($field, '.') ? $this->classMetadata->fieldMappings[$field]->originalField : $field;
$className = $this->classMetadata->name;

assert(is_string($fieldName));

Check failure on line 67 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Function assert() should not be referenced via a fallback global name, but via a use statement.

Check failure on line 67 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Function is_string() should not be referenced via a fallback global name, but via a use statement.

if (isset($this->classMetadata->fieldMappings[$field]) && $this->classMetadata->fieldMappings[$field]->originalClass !== null) {
$className = $this->classMetadata->fieldMappings[$field]->originalClass;
} elseif (isset($this->classMetadata->fieldMappings[$field]) && $this->classMetadata->fieldMappings[$field]->declared !== null) {
Expand All @@ -70,6 +76,7 @@ public function offsetGet($field): mixed
$className = $this->classMetadata->embeddedClasses[$field]->declared;
}

/** @psalm-suppress ArgumentTypeCoercion */
$this->reflFields[$field] = $this->getAccessibleProperty($className, $fieldName);

if (isset($this->classMetadata->fieldMappings[$field])) {
Expand All @@ -81,18 +88,22 @@ public function offsetGet($field): mixed
}

if ($this->classMetadata->fieldMappings[$field]->originalField !== null) {
$parentField = str_replace('.' . $fieldName, '', $field);
$parentField = str_replace('.' . $fieldName, '', $field);
$originalClass = $this->classMetadata->fieldMappings[$field]->originalClass;

if (! str_contains($parentField, '.')) {
$parentClass = $this->classMetadata->name;
} else {
$parentClass = $this->classMetadata->fieldMappings[$parentField]->originalClass;
}

/** @psalm-var class-string $parentClass */
/** @psalm-var class-string $originalClass */

$this->reflFields[$field] = new ReflectionEmbeddedProperty(
$this->getAccessibleProperty($parentClass, $parentField),

Check failure on line 104 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

ArgumentTypeCoercion

src/Mapping/LegacyReflectionFields.php:104:54: ArgumentTypeCoercion: Argument 1 of Doctrine\ORM\Mapping\LegacyReflectionFields::getAccessibleProperty expects class-string, but parent type null|string provided (see https://psalm.dev/193)

Check failure on line 104 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

PossiblyNullArgument

src/Mapping/LegacyReflectionFields.php:104:54: PossiblyNullArgument: Argument 1 of Doctrine\ORM\Mapping\LegacyReflectionFields::getAccessibleProperty cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 104 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

ArgumentTypeCoercion

src/Mapping/LegacyReflectionFields.php:104:54: ArgumentTypeCoercion: Argument 1 of Doctrine\ORM\Mapping\LegacyReflectionFields::getAccessibleProperty expects class-string, but parent type null|string provided (see https://psalm.dev/193)

Check failure on line 104 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

PossiblyNullArgument

src/Mapping/LegacyReflectionFields.php:104:54: PossiblyNullArgument: Argument 1 of Doctrine\ORM\Mapping\LegacyReflectionFields::getAccessibleProperty cannot be null, possibly null value provided (see https://psalm.dev/078)
$this->reflFields[$field],
$this->classMetadata->fieldMappings[$field]->originalClass,
$originalClass,
);
}
}
Expand All @@ -104,33 +115,36 @@ public function offsetGet($field): mixed
}

/**
* @param string $offset
* @param string $offset
* @param ReflectionProperty $value
*/
public function offsetSet($offset, $value): void
public function offsetSet($offset, $value): void // phpcs:ignore
{
$this->reflFields[$offset] = $value;
}

/** @param string $offset */
public function offsetUnset($offset): void
public function offsetUnset($offset): void // phpcs:ignore
{
unset($this->reflFields[$offset]);
}

/** @psalm-param class-string $class */
private function getAccessibleProperty(string $class, string $field): ReflectionProperty|null
private function getAccessibleProperty(string $class, string $field): ReflectionProperty
{
$reflectionProperty = $this->reflectionService->getAccessibleProperty($class, $field);
if ($reflectionProperty?->isReadOnly()) {

assert($reflectionProperty !== null);

Check failure on line 137 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Function assert() should not be referenced via a fallback global name, but via a use statement.

if ($reflectionProperty->isReadOnly()) {
$declaringClass = $reflectionProperty->class;
if ($declaringClass !== $class) {
$reflectionProperty = $this->reflectionService->getAccessibleProperty($declaringClass, $field);
}

if ($reflectionProperty !== null) {
$reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
assert($reflectionProperty !== null);

Check failure on line 144 in src/Mapping/LegacyReflectionFields.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Function assert() should not be referenced via a fallback global name, but via a use statement.
}

$reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
}

return $reflectionProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EmbeddablePropertyAccessor implements PropertyAccessor
public function __construct(
private PropertyAccessor $parent,
private PropertyAccessor $child,
/** @var class-string */
private string $embeddedClass,
) {
}
Expand Down
23 changes: 10 additions & 13 deletions src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/** @internal */
class EnumPropertyAccessor implements PropertyAccessor
{
/** @param class-string<BackedEnum> $enumType */
public function __construct(private PropertyAccessor $parent, private string $enumType)
{
}
Expand All @@ -37,7 +38,12 @@ public function getValue(object $object): mixed
return $this->fromEnum($enum);
}

private function fromEnum(array|BackedEnum $enum): int|string|array
/**
* @param BackedEnum|BackedEnum[] $enum
*
* @return ($enum is BackedEnum ? (string|int) : (string[]|int[]))
*/
private function fromEnum($enum)

Check failure on line 46 in src/Mapping/PropertyAccessors/EnumPropertyAccessor.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Method \Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor::fromEnum() does not have native type hint for its parameter $enum but it should be possible to add it based on @param annotation "BackedEnum|BackedEnum[]".
{
if (is_array($enum)) {
return array_map(static function (BackedEnum $enum) {
Expand All @@ -49,22 +55,13 @@ private function fromEnum(array|BackedEnum $enum): int|string|array
}

/**
* @param int|string|int[]|string[]|BackedEnum|BackedEnum[] $value
* @param int|string|int[]|string[] $value
*
* @return ($value is int|string|BackedEnum ? BackedEnum : BackedEnum[])
* @return ($value is int|string ? BackedEnum : BackedEnum[])
*/
private function toEnum(int|string|array|BackedEnum $value)
private function toEnum($value)

Check failure on line 62 in src/Mapping/PropertyAccessors/EnumPropertyAccessor.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.3)

Method \Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor::toEnum() does not have native type hint for its parameter $value but it should be possible to add it based on @param annotation "int|string|int[]|string[]".
{
if ($value instanceof BackedEnum) {
return $value;
}

if (is_array($value)) {
$v = reset($value);
if ($v instanceof BackedEnum) {
return $value;
}

return array_map([$this->enumType, 'from'], $value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/** @internal */
class ObjectCastPropertyAccessor implements PropertyAccessor
{
/** @param class-string $class */
public static function fromNames(string $class, string $name): self
{
$reflectionProperty = new ReflectionProperty($class, $name);
Expand Down

0 comments on commit 3a1eb2e

Please sign in to comment.