Skip to content

Commit

Permalink
Merge pull request #3217 from neos/task/phpStanLevel2
Browse files Browse the repository at this point in the history
TASK: Level up to phpstan 2 (Flow 9 adjustments)
  • Loading branch information
mhsdesign committed Jan 24, 2024
2 parents cf29e48 + b0534ef commit 3ca08aa
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
12 changes: 5 additions & 7 deletions Neos.Flow/Classes/Command/ResourceCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ public function cleanCommand()
$this->output->progressFinish();
$this->outputLine();

// FIXME flow has no dependency on Neos.Media. This code should be extracted.
/* @var AssetRepository|null $assetRepository */
$assetRepository = class_exists(AssetRepository::class) ? $this->objectManager->get(AssetRepository::class) : null;
/* @var ThumbnailRepository|null $thumbnailRepository */
$thumbnailRepository = class_exists(ThumbnailRepository::class) ? $this->objectManager->get(ThumbnailRepository::class) : null;
// FIXME flow has no dependency on Neos.Media. This code should be extracted. https://github.com/neos/flow-development-collection/issues/3272
$assetRepository = $this->objectManager->get(AssetRepository::class);
$thumbnailRepository = $this->objectManager->get(ThumbnailRepository::class);
$mediaPackagePresent = $this->packageManager->isPackageAvailable('Neos.Media');

if (count($brokenResources) > 0) {
foreach ($brokenResources as $key => $resourceIdentifier) {
$resource = $this->resourceRepository->findByIdentifier($resourceIdentifier);
$brokenResources[$key] = $resource;
if ($mediaPackagePresent && $assetRepository !== null && $thumbnailRepository !== null) {
if ($mediaPackagePresent) {
$assets = $assetRepository->findByResource($resource);
if ($assets !== null) {
$relatedAssets[$resource] = $assets;
Expand Down Expand Up @@ -293,7 +291,7 @@ public function cleanCommand()
]);
$resource->disableLifecycleEvents();
$this->resourceRepository->remove($resource);
if ($mediaPackagePresent && $assetRepository !== null && $thumbnailRepository !== null) {
if ($mediaPackagePresent) {
if (isset($relatedAssets[$resource])) {
foreach ($relatedAssets[$resource] as $asset) {
$assetRepository->removeWithoutUsageChecks($asset);
Expand Down
2 changes: 0 additions & 2 deletions Neos.Flow/Classes/I18n/Xliff/Service/XliffReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function readFiles($sourcePath, callable $iterator)
$reader->open($sourcePath);
$reader->read();

/** @var object|\XMLReader $reader the stubs for XMLReader are wrong https://github.com/phpstan/phpstan/issues/8629 */
if ($reader->nodeType == \XMLReader::ELEMENT && $reader->name === 'xliff') {
$version = $reader->getAttribute('version');
$result = true;
Expand Down Expand Up @@ -67,7 +66,6 @@ public function readFiles($sourcePath, callable $iterator)
*/
protected function isFileNode(\XMLReader $reader)
{
/** @var object|\XMLReader $reader the stubs for XMLReader are wrong https://github.com/phpstan/phpstan/issues/8629 */
return $reader->nodeType === \XMLReader::ELEMENT && $reader->name === 'file';
}
}
1 change: 1 addition & 0 deletions Neos.Flow/Classes/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function boot(Core\Bootstrap $bootstrap)
if (!$request instanceof Mvc\ActionRequest || SecurityHelper::hasSafeMethod($request->getHttpRequest()) !== true) {
$bootstrap->getObjectManager()->get(Persistence\PersistenceManagerInterface::class)->persistAll();
} elseif (SecurityHelper::hasSafeMethod($request->getHttpRequest())) {
/** @phpstan-ignore-next-line the persistence manager interface doesn't specify this method */
$bootstrap->getObjectManager()->get(Persistence\PersistenceManagerInterface::class)->persistAllowedObjects();
}
}
Expand Down
16 changes: 11 additions & 5 deletions Neos.Flow/Classes/Reflection/ReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Persistence\Proxy as DoctrineProxy;
use Neos\Cache\Backend\FreezableBackendInterface;
use Neos\Cache\Exception;
use Neos\Cache\Frontend\StringFrontend;
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\ApplicationContext;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\Proxy\ProxyInterface;
use Neos\Flow\Package;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Persistence\RepositoryInterface;
use Neos\Flow\Reflection\Exception\ClassLoadingForReflectionFailedException;
Expand Down Expand Up @@ -1676,6 +1676,7 @@ protected function convertParameterReflectionToArray(ParameterReflection $parame
$parameterInformation[self::DATA_PARAMETER_ALLOWS_NULL] = true;
}

/** @var \ReflectionNamedType|\ReflectionUnionType|\ReflectionIntersectionType|null $parameterType */
$parameterType = $parameter->getType();
if ($parameterType !== null) {
if ($parameterType instanceof \ReflectionUnionType) {
Expand Down Expand Up @@ -1739,7 +1740,6 @@ static function (\ReflectionNamedType $type) {
protected function forgetChangedClasses(): void
{
$frozenNamespaces = [];
/** @var $package Package */
foreach ($this->packageManager->getAvailablePackages() as $packageKey => $package) {
if ($this->packageManager->isPackageFrozen($packageKey)) {
$frozenNamespaces = array_merge($frozenNamespaces, $package->getNamespaces());
Expand Down Expand Up @@ -2061,8 +2061,12 @@ protected function saveProductionData(): void
$this->reflectionDataRuntimeCache->set('__classNames', $classNames);
$this->reflectionDataRuntimeCache->set('__annotatedClasses', $this->annotatedClasses);

$this->reflectionDataRuntimeCache->getBackend()->freeze();
$this->classSchemataRuntimeCache->getBackend()->freeze();
if ($this->reflectionDataRuntimeCache->getBackend() instanceof FreezableBackendInterface) {
$this->reflectionDataRuntimeCache->getBackend()->freeze();
}
if ($this->classSchemataRuntimeCache->getBackend() instanceof FreezableBackendInterface) {
$this->classSchemataRuntimeCache->getBackend()->freeze();
}

$this->log(sprintf('Built and froze reflection runtime caches (%s classes).', count($this->classReflectionData)), LogLevel::INFO);
}
Expand Down Expand Up @@ -2130,6 +2134,8 @@ protected function getPrecompiledReflectionStoragePath(): string

protected function hasFrozenCacheInProduction(): bool
{
return $this->environment->getContext()->isProduction() && $this->reflectionDataRuntimeCache->getBackend()->isFrozen();
return $this->environment->getContext()->isProduction()
&& $this->reflectionDataRuntimeCache->getBackend() instanceof FreezableBackendInterface
&& $this->reflectionDataRuntimeCache->getBackend()->isFrozen();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public function setDefaultPackageKey($defaultPackageKey)
*/
public function process(NodeInterface $node, $interceptorPosition, ParsingState $parsingState)
{
/** @var TextNode $node */
if (!$node instanceof TextNode) {
return $node;
}
if (strpos($node->getText(), 'Public/') === false) {
return $node;
}
Expand Down
15 changes: 0 additions & 15 deletions Neos.FluidAdaptor/Classes/View/TemplatePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,6 @@ protected function sanitizePath($path)
return $path;
}

/**
* @param string $packageKey
* @return string|null
*/
protected function getPackagePrivateResourcesPath($packageKey)
{
if (!$this->packageManager->isPackageAvailable($packageKey)) {
return null;
}
/** @phpstan-ignore-next-line this code will be dropped totally as its unused */
$packageResourcesPath = $this->packageManager->getPackage($packageKey)->getResourcesPath();

return Files::concatenatePaths([$packageResourcesPath, 'Private']);
}

/**
* Processes following placeholders inside $pattern:
* - "@templateRoot"
Expand Down
32 changes: 30 additions & 2 deletions bootstrap-phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@
* This bootstrap helps phpstan to detect all available constants
*/

$_SERVER['FLOW_ROOTPATH'] = dirname(__DIR__, 2);
namespace {
$_SERVER['FLOW_ROOTPATH'] = dirname(__DIR__, 2);

new \Neos\Flow\Core\Bootstrap('Testing');
new \Neos\Flow\Core\Bootstrap('Testing');
}

// FIXME flow has no dependency on Neos.Media. This code should be extracted. https://github.com/neos/flow-development-collection/issues/3272
// Theses stubs below allow phpstan to work correctly, if Neos is not installed while linting.
namespace Neos\Media\Domain\Repository {
if (!class_exists(AssetRepository::class)) {
/**
* @method iterable<int, \Neos\Media\Domain\Model\AssetInterface> findByResource(\Neos\Flow\ResourceManagement\PersistentResource $resource)
* @method void removeWithoutUsageChecks(\Neos\Media\Domain\Model\AssetInterface $object)
*/
class AssetRepository extends \Neos\Flow\Persistence\Repository
{
}
}
}

namespace Neos\Media\Domain\Repository {
if (!class_exists(ThumbnailRepository::class)) {
/**
* @method iterable<int,\Neos\Media\Domain\Model\Thumbnail> findByResource(\Neos\Flow\ResourceManagement\PersistentResource $resource)
* @method void remove(\Neos\Media\Domain\Model\Thumbnail $object)
*/
class ThumbnailRepository extends \Neos\Flow\Persistence\Repository
{
}
}
}

0 comments on commit 3ca08aa

Please sign in to comment.