Skip to content

Commit

Permalink
SDK-5463 update phpstan evaluator (#271)
Browse files Browse the repository at this point in the history
* SDK-5463 update phpstan evaluator

* SDK-5463 update tests

* SDK-5463 uncomment services back

* SDK-5463 update tests

* SDK-5463 add additional check

---------

Co-authored-by: Dmytro Klyman <dmytro.klyman@spryker.com>
  • Loading branch information
sergeyspryker and DmytroKlymanSpryker authored Dec 7, 2023
1 parent ac8ee14 commit f2a84ed
Show file tree
Hide file tree
Showing 20 changed files with 1,049 additions and 67 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"spryker-sdk/azure-php-client": "dev-master as 1.0.0",
"spryker-sdk/integrator": "dev-master as 1.0.0",
"spryker-sdk/sdk-contracts": "^0.4.7",
"spryker-sdk/utils": "^0.1.2",
"symfony/config": "^5.4",
"symfony/console": "^5.4",
"symfony/dependency-injection": "^5.4",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/DynamicEvaluator/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ services:
- '@PhpParser\ParserFactory'

PhpParser\ParserFactory:

DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateFetcher:
arguments: [ '@Upgrade\Infrastructure\PackageManager\Reader\ComposerLockReader' ]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\Dto\ViolationDto;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\FileErrorsFetcher\FileErrorsFetcherInterface;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModuleComparerInterface;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesDirsFetcherInterface;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateFetcherInterface;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateStorage;

class BrokenPhpFilesChecker
{
Expand All @@ -19,27 +23,108 @@ class BrokenPhpFilesChecker
*/
protected FileErrorsFetcherInterface $fileErrorsFetcher;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModuleComparerInterface
*/
protected SprykerModuleComparerInterface $sprykerModuleComparer;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateStorage
*/
protected SprykerModulesStateStorage $sprykerModulesStateStorage;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateFetcherInterface
*/
protected SprykerModulesStateFetcherInterface $sprykerModulesStateFetcher;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesDirsFetcherInterface
*/
protected SprykerModulesDirsFetcherInterface $sprykerModulesDirsFetcher;

/**
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\FileErrorsFetcher\FileErrorsFetcherInterface $fileErrorsFetcher
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModuleComparerInterface $sprykerModuleComparer
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateStorage $sprykerModulesStateStorage
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesStateFetcherInterface $sprykerModulesStateFetcher
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule\SprykerModulesDirsFetcherInterface $sprykerModulesDirsFetcher
*/
public function __construct(FileErrorsFetcherInterface $fileErrorsFetcher)
{
public function __construct(
FileErrorsFetcherInterface $fileErrorsFetcher,
SprykerModuleComparerInterface $sprykerModuleComparer,
SprykerModulesStateStorage $sprykerModulesStateStorage,
SprykerModulesStateFetcherInterface $sprykerModulesStateFetcher,
SprykerModulesDirsFetcherInterface $sprykerModulesDirsFetcher
) {
$this->fileErrorsFetcher = $fileErrorsFetcher;
$this->sprykerModuleComparer = $sprykerModuleComparer;
$this->sprykerModulesStateStorage = $sprykerModulesStateStorage;
$this->sprykerModulesStateFetcher = $sprykerModulesStateFetcher;
$this->sprykerModulesDirsFetcher = $sprykerModulesDirsFetcher;
}

/**
* @param array<string> $composerCommands
*
* @return array<\DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\Dto\ViolationDto>
*/
public function check(array $composerCommands): array
public function checkUpdatedSprykerModules(array $composerCommands): array
{
$fileErrors = $this->fileErrorsFetcher->fetchProjectFileErrorsAndSaveInBaseLine();
$currentModulesState = $this->sprykerModulesStateFetcher->fetchCurrentSprykerModulesState();
$previousModulesState = $this->sprykerModulesStateStorage->getModulesState();

$updatedModules = $this->sprykerModuleComparer->compareForUpdatedModules($previousModulesState, $currentModulesState);

if (count($updatedModules) === 0) {
return [];
}

$modulesDirsToCheck = $this->sprykerModulesDirsFetcher->fetchModulesDirs($updatedModules);

if (count($modulesDirsToCheck) === 0) {
return [];
}

$fileErrors = $this->fileErrorsFetcher->fetchProjectFileErrorsAndSaveInBaseLine($modulesDirsToCheck);

if (count($fileErrors) === 0) {
return [];
}

return [new ViolationDto($composerCommands, $fileErrors)];
}

/**
* @return array<\DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\Dto\ViolationDto>
*/
public function checkAll(): array
{
$fileErrors = $this->fileErrorsFetcher->fetchProjectFileErrorsAndSaveInBaseLine();

if (count($fileErrors) === 0) {
return [];
}

return [new ViolationDto([], $fileErrors)];
}

/**
* @return void
*/
public function fetchAndPersistInstalledSprykerModules(): void
{
$modulesState = $this->sprykerModulesStateFetcher->fetchCurrentSprykerModulesState();

$this->sprykerModulesStateStorage->setModulesState($modulesState);
}

/**
* @return void
*/
public function fetchAndPersistInitialErrors(): void
{
$this->fileErrorsFetcher->reset();
$this->fileErrorsFetcher->fetchProjectFileErrorsAndSaveInBaseLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\EventSubscriber;

use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\BrokenPhpFilesChecker;
use DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\FileErrorsFetcher\FileErrorsFetcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Upgrade\Application\Provider\ConfigurationProviderInterface;
use Upgrade\Application\Strategy\ReleaseApp\Processor\Event\ReleaseGroupProcessorEvent;
Expand All @@ -23,28 +22,20 @@ class BrokenPhpFilesCheckerEventSubscriber implements EventSubscriberInterface
*/
protected ConfigurationProviderInterface $configurationProvider;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\FileErrorsFetcher\FileErrorsFetcherInterface
*/
protected FileErrorsFetcherInterface $fileErrorsFetcher;

/**
* @var \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\BrokenPhpFilesChecker
*/
protected BrokenPhpFilesChecker $brokenPhpFilesChecker;

/**
* @param \Upgrade\Application\Provider\ConfigurationProviderInterface $configurationProvider
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\FileErrorsFetcher\FileErrorsFetcherInterface $fileErrorsFetcher
* @param \DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\BrokenPhpFilesChecker $brokenPhpFilesChecker
*/
public function __construct(
ConfigurationProviderInterface $configurationProvider,
FileErrorsFetcherInterface $fileErrorsFetcher,
BrokenPhpFilesChecker $brokenPhpFilesChecker
) {
$this->configurationProvider = $configurationProvider;
$this->fileErrorsFetcher = $fileErrorsFetcher;
$this->brokenPhpFilesChecker = $brokenPhpFilesChecker;
}

Expand All @@ -53,10 +44,11 @@ public function __construct(
*/
public static function getSubscribedEvents(): array
{
// TODO: SDK-5303 uncomment to activate checker
return [
// ReleaseGroupProcessorEvent::PRE_PROCESSOR => 'onPreProcessor',
// ReleaseGroupProcessorPostRequireEvent::POST_REQUIRE => 'onPostRequire',
ReleaseGroupProcessorEvent::PRE_PROCESSOR => 'onPreProcessor',
ReleaseGroupProcessorEvent::PRE_REQUIRE => 'onPreRequire',
ReleaseGroupProcessorPostRequireEvent::POST_REQUIRE => 'onPostRequire',
ReleaseGroupProcessorEvent::POST_PROCESSOR => 'onPostProcessor',
];
}

Expand All @@ -71,8 +63,21 @@ public function onPreProcessor(ReleaseGroupProcessorEvent $event): void
return;
}

$this->fileErrorsFetcher->reset();
$this->fileErrorsFetcher->fetchProjectFileErrorsAndSaveInBaseLine();
$this->brokenPhpFilesChecker->fetchAndPersistInitialErrors();
}

/**
* @param \Upgrade\Application\Strategy\ReleaseApp\Processor\Event\ReleaseGroupProcessorEvent $event
*
* @return void
*/
public function onPreRequire(ReleaseGroupProcessorEvent $event): void
{
if (!$this->configurationProvider->isEvaluatorEnabled()) {
return;
}

$this->brokenPhpFilesChecker->fetchAndPersistInstalledSprykerModules();
}

/**
Expand All @@ -88,7 +93,29 @@ public function onPostRequire(ReleaseGroupProcessorPostRequireEvent $event): voi

$stepsExecutorDto = $event->getStepsExecutionDto();

$violations = $this->brokenPhpFilesChecker->check($event->getPackageManagerResponseDto()->getExecutedCommands());
$violations = $this->brokenPhpFilesChecker->checkUpdatedSprykerModules(
$event->getPackageManagerResponseDto()->getExecutedCommands(),
);

foreach ($violations as $violation) {
$stepsExecutorDto->addViolation($violation);
}
}

/**
* @param \Upgrade\Application\Strategy\ReleaseApp\Processor\Event\ReleaseGroupProcessorEvent $event
*
* @return void
*/
public function onPostProcessor(ReleaseGroupProcessorEvent $event): void
{
if (!$this->configurationProvider->isEvaluatorEnabled()) {
return;
}

$stepsExecutorDto = $event->getStepsExecutionDto();

$violations = $this->brokenPhpFilesChecker->checkAll();

foreach ($violations as $violation) {
$stepsExecutorDto->addViolation($violation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ public function __construct(
}

/**
* @param array<string> $dirs
*
* @return array<\DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\Dto\FileErrorDto>
*/
public function fetchProjectFileErrorsAndSaveInBaseLine(): array
public function fetchProjectFileErrorsAndSaveInBaseLine(array $dirs = []): array
{
$fileErrors = [];

try {
$errors = $this->fetchErrorsArray();
$errors = $this->fetchErrorsArray($dirs);
} catch (ProcessTimedOutException $e) {
$this->logger->debug($e->getMessage());

Expand Down Expand Up @@ -156,11 +158,13 @@ protected function fetchNewFileError(string $file, array $message): ?FileErrorDt
}

/**
* @param array<string> $dirs
*
* @throws \RuntimeException
*
* @return array<mixed>
*/
protected function fetchErrorsArray(): array
protected function fetchErrorsArray(array $dirs): array
{
$process = $this->processRunnerService->run([
$this->executable,
Expand All @@ -169,6 +173,7 @@ protected function fetchErrorsArray(): array
file_exists(getcwd() . DIRECTORY_SEPARATOR . $this->phpstanNeonFileName) ? $this->executableProjectConfig : $this->executableConfig,
'--error-format',
'prettyJson',
...$dirs,
]);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
interface FileErrorsFetcherInterface
{
/**
* @param array<string> $dirs
*
* @return array<\DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\Dto\FileErrorDto>
*/
public function fetchProjectFileErrorsAndSaveInBaseLine(): array;
public function fetchProjectFileErrorsAndSaveInBaseLine(array $dirs = []): array;

/**
* @return void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

declare(strict_types=1);

namespace DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule;

class SprykerModuleComparer implements SprykerModuleComparerInterface
{
/**
* @param array<string, string> $previousSprykerModules array{ name: version }
* @param array<string, string> $newSprykerModules array{ name: version }
*
* @return array<string>
*/
public function compareForUpdatedModules(array $previousSprykerModules, array $newSprykerModules): array
{
$modulesDiff = [];

foreach ($newSprykerModules as $moduleName => $moduleVersion) {
if (!isset($previousSprykerModules[$moduleName])) {
continue;
}

if ($previousSprykerModules[$moduleName] === $moduleVersion) {
continue;
}

$modulesDiff[] = $moduleName;
}

return array_unique($modulesDiff);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

declare(strict_types=1);

namespace DynamicEvaluator\Application\Checker\BrokenPhpFilesChecker\SprykerModule;

interface SprykerModuleComparerInterface
{
/**
* @param array<string, string> $previousSprykerModules array{ name: version }
* @param array<string, string> $newSprykerModules array{ name: version }
*
* @return array<string>
*/
public function compareForUpdatedModules(array $previousSprykerModules, array $newSprykerModules): array;
}
Loading

0 comments on commit f2a84ed

Please sign in to comment.