From a061c9167a0bf91c9383981e7ca597c593ee9557 Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Thu, 5 Dec 2024 15:35:23 +0100 Subject: [PATCH] fix phpstan issues --- phpstan.neon | 4 ++- src/Builder/ExtJsDataBuilder.php | 30 ++++++++------------ src/Controller/Admin/SettingsController.php | 7 +++-- src/Event/SocialPostBuildConfigureEvent.php | 3 -- src/Form/Admin/Type/Wall/FeedType.php | 7 +++-- src/Manager/ConnectorManager.php | 4 --- src/Manager/SocialPostManager.php | 2 +- src/Processor/SocialPostBuilderProcessor.php | 14 ++------- src/Repository/SocialPostRepository.php | 9 ++++-- 9 files changed, 34 insertions(+), 46 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 7519df8..de5779a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,4 +6,6 @@ parameters: reportUnmatchedIgnoredErrors: false symfony: containerXmlPath: %currentWorkingDirectory%/var/cache/test/TestKernelTestDebugContainer.xml - constantHassers: false \ No newline at end of file + constantHassers: false + ignoreErrors: + - '#Trait SocialDataBundle\\Controller\\Admin\\Traits\\ConnectResponseTrait is used zero times and is not analysed\.#' \ No newline at end of file diff --git a/src/Builder/ExtJsDataBuilder.php b/src/Builder/ExtJsDataBuilder.php index 27072c8..185a81e 100644 --- a/src/Builder/ExtJsDataBuilder.php +++ b/src/Builder/ExtJsDataBuilder.php @@ -18,24 +18,13 @@ class ExtJsDataBuilder { - protected NormalizerInterface $serializer; - protected Translator $translator; - protected ConnectorManagerInterface $connectorManager; - protected WallManagerInterface $wallManager; - protected StatisticServiceInterface $statisticService; - public function __construct( - NormalizerInterface $serializer, - Translator $translator, - ConnectorManagerInterface $connectorManager, - WallManagerInterface $wallManager, - StatisticServiceInterface $statisticService + protected NormalizerInterface $serializer, + protected Translator $translator, + protected ConnectorManagerInterface $connectorManager, + protected WallManagerInterface $wallManager, + protected StatisticServiceInterface $statisticService ) { - $this->serializer = $serializer; - $this->translator = $translator; - $this->connectorManager = $connectorManager; - $this->wallManager = $wallManager; - $this->statisticService = $statisticService; } public function generateConnectorListData(): array @@ -163,13 +152,18 @@ public function generateFormErrorList(FormInterface $form): array { $errors = []; - /** @var FormError $e */ foreach ($form->getErrors(true, true) as $e) { + + if (!$e instanceof FormError) { + continue; + } + $errorMessageTemplate = $e->getMessageTemplate(); foreach ($e->getMessageParameters() as $key => $value) { $errorMessageTemplate = str_replace($key, $value, $errorMessageTemplate); } - $errors[] = sprintf('%s: %s', $e->getOrigin()->getConfig()->getName(), $errorMessageTemplate); + + $errors[] = sprintf('%s: %s', $e->getOrigin()?->getConfig()->getName(), $errorMessageTemplate); } return $errors; diff --git a/src/Controller/Admin/SettingsController.php b/src/Controller/Admin/SettingsController.php index adcb2b3..94ecdbb 100644 --- a/src/Controller/Admin/SettingsController.php +++ b/src/Controller/Admin/SettingsController.php @@ -164,10 +164,13 @@ public function saveConnectorConfigurationAction(Request $request, string $conne $connectorDefinition = $this->connectorManager->getConnectorDefinition($connectorName, true); - /** @var ConnectorEngineConfigurationInterface $class */ + $formType = ''; $class = $connectorDefinition->getEngineConfigurationClass(); + if (is_string($class) && is_subclass_of($class, ConnectorEngineConfigurationInterface::class)) { + $formType = $class::getFormClass(); + } - $form = $this->formFactory->create($class::getFormClass(), $connectorDefinition->getEngineConfiguration()); + $form = $this->formFactory->create($formType, $connectorDefinition->getEngineConfiguration()); $form->submit($configuration); diff --git a/src/Event/SocialPostBuildConfigureEvent.php b/src/Event/SocialPostBuildConfigureEvent.php index d1bd4a5..3411605 100644 --- a/src/Event/SocialPostBuildConfigureEvent.php +++ b/src/Event/SocialPostBuildConfigureEvent.php @@ -28,9 +28,6 @@ public function getBuildConfig(): BuildConfig return $this->buildConfig; } - /** - * @throws \Exception - */ public function setOption(mixed $key, mixed $value): void { $this->options[$key] = $value; diff --git a/src/Form/Admin/Type/Wall/FeedType.php b/src/Form/Admin/Type/Wall/FeedType.php index acff698..2838c22 100644 --- a/src/Form/Admin/Type/Wall/FeedType.php +++ b/src/Form/Admin/Type/Wall/FeedType.php @@ -66,10 +66,13 @@ public function addConfigurationField(FormEvent $event): void throw new InvalidConfigurationException(sprintf('No Connector definition found for engine "%s"', $connectorEngine->getName())); } - /** @var ConnectorFeedConfigurationInterface $class */ + $formType = ''; $class = $connectorDefinition->getFeedConfigurationClass(); + if (is_subclass_of($class, ConnectorFeedConfigurationInterface::class)) { + $formType = $class::getFormClass(); + } - $form->add('configuration', $class::getFormClass()); + $form->add('configuration', $formType); } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/Manager/ConnectorManager.php b/src/Manager/ConnectorManager.php index 23597aa..3e34d5b 100644 --- a/src/Manager/ConnectorManager.php +++ b/src/Manager/ConnectorManager.php @@ -62,10 +62,6 @@ public function getConnectorDefinition(string $connectorDefinitionName, bool $lo return null; } - if (!$connectorDefinition instanceof ConnectorDefinitionInterface) { - return null; - } - if ($loadEngine === true) { $connectorEngine = $this->connectorEngineRepository->findByName($connectorDefinitionName); $connectorDefinition->setConnectorEngine($connectorEngine); diff --git a/src/Manager/SocialPostManager.php b/src/Manager/SocialPostManager.php index 647260a..2606042 100644 --- a/src/Manager/SocialPostManager.php +++ b/src/Manager/SocialPostManager.php @@ -144,7 +144,7 @@ protected function persistMedia(FeedInterface $feed, SocialPostInterface $social // 2. forced mode enabled or new asset // => add new data or try to add new version if data has changed! - if ($isNewAsset === false && $forceProcessing === true && md5($asset->getData()) === md5($imageData['content'])) { + if ($forceProcessing === true && md5($asset->getData()) === md5($imageData['content'])) { $socialPost->setPoster($asset); $this->logger->debug( sprintf('Asset %s for post %s forced update skipped since no data has been changed', $asset->getFilename(), $socialPost->getSocialId()), diff --git a/src/Processor/SocialPostBuilderProcessor.php b/src/Processor/SocialPostBuilderProcessor.php index fd572a1..756807e 100644 --- a/src/Processor/SocialPostBuilderProcessor.php +++ b/src/Processor/SocialPostBuilderProcessor.php @@ -95,10 +95,6 @@ protected function processWall(WallInterface $wall, bool $forceProcessing): void protected function processFeed(FeedInterface $feed, bool $forceProcessing): void { $connectorEngine = $feed->getConnectorEngine(); - if (!$connectorEngine instanceof ConnectorEngineInterface) { - // @todo: dispatch notification? - return; - } if (!$connectorEngine->isEnabled()) { // @todo: dispatch notification? @@ -146,14 +142,7 @@ protected function loadFeedPosts(string $connectorName, BuildConfig $buildConfig return []; } - $fetchedItems = $fetchData->getFetchedEntities(); - - if (!is_array($fetchedItems)) { - $this->logger->debug(sprintf('No elements found during fetch process'), $logContext); - return []; - } - - foreach ($fetchedItems as $entry) { + foreach ($fetchData->getFetchedEntities() as $entry) { // 2 filter $filterData = $this->dispatchSocialPostBuildCycle('filter', $connectorName, $buildConfig, $postBuilder, [ @@ -185,6 +174,7 @@ protected function loadFeedPosts(string $connectorName, BuildConfig $buildConfig continue; } + /* @phpstan-ignore-next-line */ if (!$preFetchedSocialPostEntity instanceof SocialPostInterface) { $this->logger->error(sprintf('Could not resolve pre-fetched social post for entity with id "%s"', $filteredId), $logContext); continue; diff --git a/src/Repository/SocialPostRepository.php b/src/Repository/SocialPostRepository.php index 2de7584..aeb70d5 100644 --- a/src/Repository/SocialPostRepository.php +++ b/src/Repository/SocialPostRepository.php @@ -230,14 +230,17 @@ public function deleteOutdatedSocialPosts(int $expireDays, bool $deletePoster = } catch (\Exception $e) { // fail silently } - } } public function getClassId(): string { - /** @var Concrete $concreteObject */ - $concreteObject = sprintf('\Pimcore\Model\DataObject\%s', ucfirst($this->environmentService->getSocialPostDataClass())); + $dataClassName = ucfirst($this->environmentService->getSocialPostDataClass()); + $concreteObject = sprintf('\Pimcore\Model\DataObject\%s', $dataClassName); + + if (!is_subclass_of($concreteObject, Concrete::class)) { + throw new \Exception(sprintf('Invalid data object class "%s"', $dataClassName)); + } return $concreteObject::classId(); }