Skip to content

Commit

Permalink
fix pr #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 3, 2025
1 parent f8800e2 commit 6262542
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
42 changes: 38 additions & 4 deletions src/ReflectionParameterTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ public function __construct(
private ReflectionParameter $reflection
) {
$this->type = $this->getType();
$parameter = ($this->type instanceof ReflectionUnionType) ?
toUnionParameter($this->type->getTypes()) :
toParameter($this->type?->getName() ?? 'mixed');
$parameter = $this->getParameter();

try {
$attribute = reflectedParameterAttribute($reflection);
} catch (Throwable) {
// do nothing
}
if (isset($attribute, $this->type)) {
$typeHint = $this->type->getName();
$typeHint = $this->getTypeHint($this->type);
$attrHint = $attribute->parameter()->type()->typeHinting();
if ($typeHint !== $attrHint) {
throw new TypeError(
Expand Down Expand Up @@ -76,6 +74,25 @@ public function parameter(): ParameterInterface
return $this->parameter;
}

private function getParameter(): ParameterInterface
{
if ($this->type instanceof ReflectionUnionType) {
$types = [];
foreach ($this->type->getTypes() as $type) {
if ($type instanceof ReflectionIntersectionType) {

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();

Check warning on line 82 in src/ReflectionParameterTyped.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 test on ubuntu-24.04

Escaped Mutant for Mutator "InstanceOf_": @@ @@ if ($this->type instanceof ReflectionUnionType) { $types = []; foreach ($this->type->getTypes() as $type) { - if ($type instanceof ReflectionIntersectionType) { + if (false) { continue; } $types[] = $type->getName();
continue;
}
$types[] = $type->getName();
}

return toUnionParameter(...$types);
} elseif ($this->type !== null) {
return toParameter($this->getTypeHint($this->type));
}

return toParameter('mixed');
}

private function getType(): ReflectionNamedType|ReflectionUnionType|null
{
$reflection = $this->reflection->getType();
Expand All @@ -97,6 +114,23 @@ private function getType(): ReflectionNamedType|ReflectionUnionType|null
);
}

private function getTypeHint(object $reflection): string
{
if (method_exists($reflection, 'getName')) {
return $reflection->getName();
}
if ($reflection instanceof ReflectionUnionType) {
$types = [];
foreach ($reflection->getTypes() as $type) {
$types[] = $this->getTypeHint($type);
}

return implode('|', $types);
}

return 'mixed';
}

/**
* @infection-ignore-all
*/
Expand Down
8 changes: 2 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use ReflectionAttribute;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use Throwable;
use function Chevere\Message\message;
Expand Down Expand Up @@ -137,15 +136,12 @@ function assertNamedArgument(
}
}

function toUnionParameter(array $types): UnionParameterInterface
function toUnionParameter(string ...$types): UnionParameterInterface
{
$parameters = [];

/** @var ReflectionNamedType $type */
foreach ($types as $type) {
$parameters[] = toParameter($type->getName());
$parameters[] = toParameter($type);
}

$parameters = parameters(...$parameters);

return new UnionParameter($parameters);
Expand Down

0 comments on commit 6262542

Please sign in to comment.