Skip to content

Commit

Permalink
Add property value extractor methods for PHPStan level 9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schmitt committed Jun 28, 2024
1 parent 88a3032 commit 5e58d56
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Classes/Fusion/AbstractComponentPresentationObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace PackageFactory\AtomicFusion\PresentationObjects\Fusion;

use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
Expand All @@ -29,6 +30,65 @@ abstract class AbstractComponentPresentationObjectFactory implements
#[Flow\Inject]
protected Translator $translator;

/**
* @template T
* @param class-string<T> $expectedType
* @return ?T
*/
final protected static function getObjectValue(Node $node, string $propertyName, string $expectedType): mixed
{
$propertyValue = $node->getProperty($propertyName);

return $propertyValue instanceof $expectedType
? $propertyValue
: null;
}

/**
* @template T
* @param class-string<T> $expectedType
* @return array<int,T>|null
*/
final protected static function getObjectArrayValue(Node $node, string $propertyName, string $expectedType): ?array
{
$propertyValue = $node->getProperty($propertyName);
if (!is_array($propertyValue)) {
return null;
}
return array_filter(
$propertyValue,
fn (mixed $item): bool => $item instanceof $expectedType
);
}

final protected static function getStringValue(Node $node, string $propertyName): ?string
{
$propertyValue = $node->getProperty($propertyName);

return is_string($propertyValue) ? $propertyValue : null;
}

final protected static function getBoolValue(Node $node, string $propertyName): ?bool
{
$propertyValue = $node->getProperty($propertyName);

return is_bool($propertyValue) ? $propertyValue : null;
}

final protected static function getIntValue(Node $node, string $propertyName): ?int
{
$propertyValue = $node->getProperty($propertyName);

return is_int($propertyValue) ? $propertyValue : null;
}

final protected static function getFloatValue(Node $node, string $propertyName): ?float
{
$propertyValue = $node->getProperty($propertyName);

return is_float($propertyValue) ? $propertyValue : null;
}

/**
* All methods are considered safe
*/
Expand Down

0 comments on commit 5e58d56

Please sign in to comment.