Skip to content

Commit

Permalink
Merge pull request #4650 from neos/task/phpstanInDerBahn
Browse files Browse the repository at this point in the history
TASK: Add phpstan to all `Neos.ContentRepository*` packages
  • Loading branch information
mhsdesign authored Nov 2, 2023
2 parents 1b0a1ba + d496755 commit 2b878a4
Show file tree
Hide file tree
Showing 67 changed files with 204 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public function has(string $projectionClassName): bool
return array_key_exists($projectionClassName, $this->projections);
}

/**
* @return list<class-string<ProjectionInterface<ProjectionStateInterface>>>
*/
public function getClassNames(): array
{
return array_keys($this->projections);
}

/**
* @return \Traversable<ProjectionInterface<ProjectionStateInterface>>
*/
Expand Down
2 changes: 1 addition & 1 deletion Neos.ContentRepository.Core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"neos/utility-objecthandling": "*",
"neos/utility-arrays": "*",
"doctrine/dbal": "^2.6",
"symfony/serializer": "*",
"symfony/serializer": "^6.3",
"psr/clock": "^1",
"behat/transliterator": "~1.0",
"ramsey/uuid": "^3.0 || ^4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public function findAssetById(string $assetId): SerializedAsset|SerializedImageV
'neos_media_adjustment_resizeimageadjustment' => ImageAdjustmentType::RESIZE_IMAGE,
'neos_media_adjustment_cropimageadjustment' => ImageAdjustmentType::CROP_IMAGE,
'neos_media_adjustment_qualityimageadjustment' => ImageAdjustmentType::QUALITY_IMAGE,
default => throw new \InvalidArgumentException(sprintf('Invalid "dtype" in image adjustment row. Got "%s"', $imageAdjustmentRow['dtype']), 1698583820)
};
#unset($imageAdjustmentRow['persistence_object_identifier'], $imageAdjustmentRow['imagevariant'], $imageAdjustmentRow['dtype']);
#$imageAdjustmentRow = array_filter($imageAdjustmentRow, static fn ($value) => $value !== null);
$imageAdjustments[] = ['type' => $type->value, 'properties' => $type->convertProperties($imageAdjustmentRow)];
}
return SerializedImageVariant::fromArray([
Expand All @@ -82,11 +81,13 @@ public function findAssetById(string $assetId): SerializedAsset|SerializedImageV
unset($row[$key]);
}
$row['type'] = match ($row['type']) {
'neos_media_image' => AssetType::IMAGE->value,
'neos_media_audio' => AssetType::AUDIO->value,
'neos_media_document' => AssetType::DOCUMENT->value,
'neos_media_video' => AssetType::VIDEO->value,
'neos_media_image' => AssetType::IMAGE->value,
'neos_media_audio' => AssetType::AUDIO->value,
'neos_media_document' => AssetType::DOCUMENT->value,
'neos_media_video' => AssetType::VIDEO->value,
default => throw new \InvalidArgumentException(sprintf('Invalid "type" in asset row. Got "%s"', $row['type']), 1698583897)
};
/** @phpstan-ignore-next-line */
return SerializedAsset::fromArray($row);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function getStreamBySha1(string $sha1)
if (!is_readable($resourcePath)) {
throw new \RuntimeException(sprintf('Resource file "%s" is not readable', $resourcePath), 1658583621);
}
return fopen($resourcePath, 'rb');
$stream = fopen($resourcePath, 'rb');
if (!is_resource($stream)) {
throw new \RuntimeException(sprintf('Could not open stream to resource file "%s"', $resourcePath), 1698584155);
}
return $stream;
}
}
1 change: 1 addition & 0 deletions Neos.ContentRepository.Export/src/Asset/AssetExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

final class AssetExporter
{
/** @var array<string, true> */
private array $exportedAssetIds = [];

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ enum ImageAdjustmentType: string
case CROP_IMAGE = 'CROP_IMAGE';
case QUALITY_IMAGE = 'QUALITY_IMAGE';

/**
* @param array<string, string> $data
* @return array<string, mixed>
*/
public function convertProperties(array $data): array
{
$data = array_change_key_case($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public static function fromAsset(Asset $asset): self
if ($resource === null) {
throw new \InvalidArgumentException(sprintf('Failed to load resource for asset "%s"', $asset->getIdentifier()), 1645871592);
}
$type = match (TypeHandling::getTypeForValue($asset)) {
$type = match ($typeForValue = TypeHandling::getTypeForValue($asset)) {
Image::class => AssetType::IMAGE,
Audio::class => AssetType::AUDIO,
Document::class => AssetType::DOCUMENT,
Video::class => AssetType::VIDEO,
default => throw new \InvalidArgumentException(sprintf('Invalid asset type "%s" for asset "%s"', $typeForValue, $asset->getIdentifier()), 1698584356)
};
return new self(
$asset->getIdentifier(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ private function __construct(

public static function fromImageAdjustment(ImageAdjustmentInterface $adjustment): self
{
$type = match(TypeHandling::getTypeForValue($adjustment)) {
$type = match($typeForValue = TypeHandling::getTypeForValue($adjustment)) {
ResizeImageAdjustment::class => ImageAdjustmentType::RESIZE_IMAGE,
CropImageAdjustment::class => ImageAdjustmentType::CROP_IMAGE,
QualityImageAdjustment::class => ImageAdjustmentType::QUALITY_IMAGE,
default => throw new \InvalidArgumentException(sprintf('Invalid image adjustment type "%s"', $typeForValue), 1698584402)
};
return new self($type, $type->convertProperties(ObjectAccess::getGettableProperties($adjustment)));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function toJson(): string
*/
public function jsonSerialize(): array
{
/** @phpstan-ignore-next-line */
return get_object_vars($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ private function __construct(
) {
}

/**
* @param iterable<ExportedEvent> $events
*/
public static function fromIterable(iterable $events): self
{
return new self(function () use ($events) {
Expand Down
7 changes: 4 additions & 3 deletions Neos.ContentRepository.Export/src/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public function __construct(

public function runAllProcessors(\Closure $outputLineFn, bool $verbose = false): void
{
/** @var array<string, ProcessorInterface> $processors */
$processors = [
'Exporting events' => new EventExportProcessor(
'Exporting events' => new EventExportProcessor(
$this->filesystem,
$this->workspaceFinder,
$this->eventStore
),
'Exporting assets' => new AssetExportProcessor(
'Exporting assets' => new AssetExportProcessor(
$this->filesystem,
$this->assetRepository,
$this->workspaceFinder,
Expand All @@ -50,7 +51,7 @@ public function runAllProcessors(\Closure $outputLineFn, bool $verbose = false):
);
$result = $processor->run();
if ($result->severity === Severity::ERROR) {
throw new \RuntimeException($label . ': ' . $result->message ?? '');
throw new \RuntimeException($label . ': ' . ($result->message ?? ''));
}
$outputLineFn(' ' . $result->message);
$outputLineFn();
Expand Down
1 change: 1 addition & 0 deletions Neos.ContentRepository.Export/src/ExportServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @internal
* @implements ContentRepositoryServiceFactoryInterface<ExportService>
*/
class ExportServiceFactory implements ContentRepositoryServiceFactoryInterface
{
Expand Down
8 changes: 6 additions & 2 deletions Neos.ContentRepository.Export/src/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function runAllProcessors(\Closure $outputLineFn, bool $verbose = false):
);
$result = $processor->run();
if ($result->severity === Severity::ERROR) {
throw new \RuntimeException($label . ': ' . $result->message ?? '');
throw new \RuntimeException($label . ': ' . ($result->message ?? ''));
}
$outputLineFn(' ' . $result->message);
$outputLineFn();
Expand All @@ -77,6 +77,10 @@ public function runAllProcessors(\Closure $outputLineFn, bool $verbose = false):
private function liveWorkspaceContentStreamExists(): bool
{
$workspaceStreamName = WorkspaceEventStreamName::fromWorkspaceName(WorkspaceName::forLive())->getEventStreamName();
return $this->eventStore->load($workspaceStreamName)->getIterator()->current() !== null;
$eventStream = $this->eventStore->load($workspaceStreamName);
foreach ($eventStream as $event) {
return true;
}
return false;
}
}
1 change: 1 addition & 0 deletions Neos.ContentRepository.Export/src/ImportServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* @internal
* @implements ContentRepositoryServiceFactoryInterface<ImportService>
*/
class ImportServiceFactory implements ContentRepositoryServiceFactoryInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
final class AssetExportProcessor implements ProcessorInterface
{
/** @var array<int, \Closure> */
private array $callbacks = [];

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
final class AssetRepositoryImportProcessor implements ProcessorInterface
{
/** @var array<int, \Closure> */
private array $callbacks = [];

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
final class EventExportProcessor implements ProcessorInterface
{
/** @var array<int, \Closure> */
private array $callbacks = [];

public function __construct(
Expand Down Expand Up @@ -64,6 +65,9 @@ public function run(): ProcessorResult
/** --------------------------------------- */


/**
* @phpstan-ignore-next-line currently this private method is unused ... but it does no harm keeping it
*/
private function dispatch(Severity $severity, string $message, mixed ...$args): void
{
$renderedMessage = sprintf($message, ...$args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
final class EventStoreImportProcessor implements ProcessorInterface
{
/** @var array<int, \Closure> */
private array $callbacks = [];

public function __construct(
Expand Down Expand Up @@ -97,7 +98,7 @@ public function run(): ProcessorResult
new Event(
EventId::fromString($event->identifier),
Event\EventType::fromString($event->type),
Event\EventData::fromString(\json_encode($event->payload)),
Event\EventData::fromString(\json_encode($event->payload, JSON_THROW_ON_ERROR)),
Event\EventMetadata::fromArray($event->metadata)
)
);
Expand Down Expand Up @@ -188,6 +189,9 @@ private static function extractContentStreamId(array $payload): ContentStreamId
return ContentStreamId::fromString($payload['contentStreamId']);
}

/**
* @phpstan-ignore-next-line currently this private method is unused ... but it does no harm keeping it
*/
private function dispatch(Severity $severity, string $message, mixed ...$args): void
{
$renderedMessage = sprintf($message, ...$args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ final class BackReferenceNodesOperation implements OperationInterface
*/
protected $contentRepositoryRegistry;

/** @param array<int, mixed> $context */
public function canEvaluate($context): bool
{
return count($context) === 0 || (isset($context[0]) && ($context[0] instanceof Node));
}

/** @param array<int, mixed> $arguments */
public function evaluate(FlowQuery $flowQuery, array $arguments): void
{
$output = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ final class BackReferencesOperation implements OperationInterface
*/
protected $contentRepositoryRegistry;

/** @param array<int, mixed> $context */
public function canEvaluate($context): bool
{
return count($context) === 0 || (isset($context[0]) && ($context[0] instanceof Node));
}

/** @param array<int, mixed> $arguments */
public function evaluate(FlowQuery $flowQuery, array $arguments): void
{
$output = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ protected function earlyOptimizationOfFilters(FlowQuery $flowQuery, array $parse
// Add filtered nodes to output
/** @var Node $filteredNode */
foreach ($filteredOutput as $filteredNode) {
/** @phpstan-ignore-next-line undefined behaviour https://github.com/neos/neos-development-collection/issues/4507#issuecomment-1784123143 */
if (!isset($outputNodeAggregateIds[$filteredNode->nodeAggregateId->value])) {
$output[] = $filteredNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Neos\Utility\NodeTypeWithFallbackProvider;
use Neos\Utility\ObjectAccess;
use Neos\Flow\Annotations as Flow;

/**
* This filter implementation contains specific behavior for use on ContentRepository
Expand All @@ -31,6 +34,11 @@
*/
class FilterOperation extends \Neos\Eel\FlowQuery\Operations\Object\FilterOperation
{
use NodeTypeWithFallbackProvider;

#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;

/**
* {@inheritdoc}
*
Expand All @@ -41,7 +49,7 @@ class FilterOperation extends \Neos\Eel\FlowQuery\Operations\Object\FilterOperat
/**
* {@inheritdoc}
*
* @param array $context $context onto which this operation should be applied (array or array-like object)
* @param array<int, mixed> $context $context onto which this operation should be applied (array or array-like object)
* @return boolean true if the operation can be applied onto the $context, false otherwise
*/
public function canEvaluate($context)
Expand All @@ -53,7 +61,7 @@ public function canEvaluate($context)
* {@inheritdoc}
*
* @param FlowQuery $flowQuery
* @param array $arguments
* @param array<int, mixed> $arguments
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments)
Expand Down Expand Up @@ -86,13 +94,8 @@ public function evaluate(FlowQuery $flowQuery, array $arguments)
*/
protected function matchesPropertyNameFilter($element, $propertyNameFilter)
{
/* @var Node $element */
try {
return ((string)$element->nodeName === $propertyNameFilter);
} catch (\InvalidArgumentException $e) {
// in case the Element has no valid node name, we do not match!
return false;
}
assert($element instanceof Node);
return $element->nodeName?->value === $propertyNameFilter;
}

/**
Expand Down Expand Up @@ -138,11 +141,12 @@ protected function evaluateOperator($value, $operator, $operand)
{
if ($operator === 'instanceof' && $value instanceof Node) {
if ($this->operandIsSimpleType($operand)) {
/** @phpstan-ignore-next-line Flow does not properly declare its types here */
return $this->handleSimpleTypeOperand($operand, $value);
} elseif ($operand === Node::class) {
return true;
} else {
$isOfType = $value->nodeType->isOfType($operand[0] === '!' ? substr($operand, 1) : $operand);
$isOfType = $this->getNodeType($value)->isOfType($operand[0] === '!' ? substr($operand, 1) : $operand);
return $operand[0] === '!' ? $isOfType === false : $isOfType;
}
} elseif ($operator === '!instanceof' && $value instanceof Node) {
Expand Down
Loading

0 comments on commit 2b878a4

Please sign in to comment.