Skip to content

Commit

Permalink
[DX] Remove PackageBuilder classes (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 1, 2022
1 parent 3c07468 commit 6cdebe4
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 23 deletions.
3 changes: 1 addition & 2 deletions build/build-preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nette\Utils\Strings;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;

$possiblePaths = [
// rector-src
Expand All @@ -28,7 +27,7 @@

$buildDirectory = $argv[1];

$symfonyStyleFactory = new SymfonyStyleFactory();
$symfonyStyleFactory = new \Rector\Core\Console\Style\SymfonyStyleFactory(new \Rector\Core\Util\Reflection\PrivatesAccessor());
$symfonyStyle = $symfonyStyleFactory->create();

if (! is_string($buildDirectory)) {
Expand Down
6 changes: 1 addition & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Console\Style\RectorConsoleOutputStyleFactory;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Validation\Collector\EmptyConfigurableRectorCollector;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
Expand All @@ -48,8 +49,6 @@
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyParallel\ValueObject\EasyParallelConfig;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\PackageBuilder\Yaml\ParametersMerger;

return static function (RectorConfig $rectorConfig): void {
// make use of https://github.com/symplify/easy-parallel
Expand Down Expand Up @@ -111,9 +110,6 @@
__DIR__ . '/../rules/*/Enum/*',
]);

// parallel
$services->set(ParametersMerger::class);

$services->set(Filesystem::class);

// use faster in-memory cache in CI.
Expand Down
4 changes: 2 additions & 2 deletions e2e/e2eTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// runs a rector e2e test.
// checks whether we expect a certain output, or alternatively that rector just processed everything without errors

use Rector\Core\Console\Style\SymfonyStyleFactory;
use SebastianBergmann\Diff\Differ;
use Symfony\Component\Console\Command\Command;
use Symplify\PackageBuilder\Console\Formatter\ColorConsoleDiffFormatter;
use Symplify\PackageBuilder\Console\Output\ConsoleDiffer;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;

$projectRoot = __DIR__ .'/..';
$rectorBin = $projectRoot . '/bin/rector';
Expand Down Expand Up @@ -40,7 +40,7 @@
exit($exitCode);
}

$symfonyStyleFactory = new SymfonyStyleFactory();
$symfonyStyleFactory = new SymfonyStyleFactory(new \Rector\Core\Util\Reflection\PrivatesAccessor());
$symfonyStyle = $symfonyStyleFactory->create();

$matchedExpectedOutput = false;
Expand Down
6 changes: 3 additions & 3 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Core\Util\ArrayParametersMerger;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Parallel\ValueObject\Bridge;
use Symplify\EasyParallel\Enum\Action;
use Symplify\EasyParallel\Enum\ReactCommand;
use Symplify\EasyParallel\Enum\ReactEvent;
use Symplify\PackageBuilder\Yaml\ParametersMerger;
use Throwable;

final class WorkerRunner
Expand All @@ -30,7 +30,7 @@ final class WorkerRunner
private const RESULT = 'result';

public function __construct(
private readonly ParametersMerger $parametersMerger,
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly CurrentFileProvider $currentFileProvider,
private readonly PhpFileProcessor $phpFileProcessor,
private readonly NodeScopeResolver $nodeScopeResolver,
Expand Down Expand Up @@ -89,7 +89,7 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura

$currentErrorsAndFileDiffs = $this->phpFileProcessor->process($file, $configuration);

$errorAndFileDiffs = $this->parametersMerger->merge(
$errorAndFileDiffs = $this->arrayParametersMerger->merge(
$errorAndFileDiffs,
$currentErrorsAndFileDiffs
);
Expand Down
16 changes: 16 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -812,3 +812,19 @@ parameters:
message: '#Offset \(int\|string\) on non\-empty\-array<PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocChildNode> in isset\(\) always exists and is not nullable#'
path: packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php
- '#Parameter \#3 \$secondCondition of method Rector\\Core\\NodeManipulator\\BinaryOpManipulator\:\:matchFirstAndSecondConditionNode\(\) expects \(callable\(PhpParser\\Node, PhpParser\\Node\=\)\: bool\)\|class\-string<PhpParser\\Node>, Closure\(PhpParser\\Node, PhpParser\\Node\)\: bool given#'

-
message: '#Method Rector\\Core\\Util\\ArrayParametersMerger\:\:mergeLeftToRightWithCallable\(\) has parameter \$mergeCallback with no signature specified for callable#'
path: src/Util/ArrayParametersMerger.php

-
message: '#Make callable type explicit\. Here is how\: https\://phpstan\.org/writing\-php\-code/phpdoc\-types\#callables#'
path: src/Util/ArrayParametersMerger.php

-
message: '#Construct empty\(\) is not allowed\. Use more strict comparison#'
path: src/Util/ArrayParametersMerger.php

-
message: '#"empty\(\$right\)" is forbidden to use#'
path: src/Util/ArrayParametersMerger.php
6 changes: 3 additions & 3 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Util\ArrayParametersMerger;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
Expand All @@ -23,7 +24,6 @@
use Symplify\EasyParallel\CpuCoreCountProvider;
use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyParallel\ScheduleFactory;
use Symplify\PackageBuilder\Yaml\ParametersMerger;
use Webmozart\Assert\Assert;

final class ApplicationFileProcessor
Expand All @@ -48,7 +48,7 @@ public function __construct(
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileFactory $fileFactory,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly ParametersMerger $parametersMerger,
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly ParallelFileProcessor $parallelFileProcessor,
private readonly ParameterProvider $parameterProvider,
private readonly ScheduleFactory $scheduleFactory,
Expand Down Expand Up @@ -125,7 +125,7 @@ public function processFiles(array $files, Configuration $configuration): array
}

$result = $fileProcessor->process($file, $configuration);
$systemErrorsAndFileDiffs = $this->parametersMerger->merge($systemErrorsAndFileDiffs, $result);
$systemErrorsAndFileDiffs = $this->arrayParametersMerger->merge($systemErrorsAndFileDiffs, $result);
}

// progress bar +1
Expand Down
53 changes: 53 additions & 0 deletions src/Console/Style/SymfonyStyleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Console\Style;

use Rector\Core\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class SymfonyStyleFactory
{
public function __construct(private readonly PrivatesAccessor $privatesAccessor)
{
}

public function create(): SymfonyStyle
{
// to prevent missing argv indexes
if (! isset($_SERVER['argv'])) {
$_SERVER['argv'] = [];
}

$argvInput = new ArgvInput();
$consoleOutput = new ConsoleOutput();

// to configure all -v, -vv, -vvv options without memory-lock to Application run() arguments
$this->privatesAccessor->callPrivateMethod(new Application(), 'configureIO', [$argvInput, $consoleOutput]);

// --debug is called
if ($argvInput->hasParameterOption('--debug')) {
$consoleOutput->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}

// disable output for tests
if ($this->isPHPUnitRun()) {
$consoleOutput->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}

return new SymfonyStyle($argvInput, $consoleOutput);
}

/**
* Never ever used static methods if not neccesary, this is just handy for tests + src to prevent duplication.
*/
private function isPHPUnitRun(): bool
{
return defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Rector\Core\DependencyInjection\Collector;

use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Util\ArrayParametersMerger;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use ReflectionClass;
use ReflectionClassConstant;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Definition;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\PackageBuilder\Yaml\ParametersMerger;

final class ConfigureCallValuesCollector
{
Expand All @@ -19,14 +20,14 @@ final class ConfigureCallValuesCollector
*/
private array $configureCallValuesByRectorClass = [];

private readonly ParametersMerger $parametersMerger;
private readonly ArrayParametersMerger $arrayParametersMerger;

private readonly SymfonyStyle $symfonyStyle;

public function __construct()
{
$this->parametersMerger = new ParametersMerger();
$symfonyStyleFactory = new SymfonyStyleFactory();
$this->arrayParametersMerger = new ArrayParametersMerger();
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor());
$this->symfonyStyle = $symfonyStyleFactory->create();
}

Expand Down Expand Up @@ -98,7 +99,7 @@ private function addConfigureCallValues(string $rectorClass, array $configureVal
if (! isset($this->configureCallValuesByRectorClass[$rectorClass])) {
$this->configureCallValuesByRectorClass[$rectorClass] = $configureValue;
} else {
$mergedParameters = $this->parametersMerger->merge(
$mergedParameters = $this->arrayParametersMerger->merge(
$this->configureCallValuesByRectorClass[$rectorClass],
$configureValue
);
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/Reflection/InvalidPrivatePropertyTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Exception\Reflection;

use Exception;

final class InvalidPrivatePropertyTypeException extends Exception
{
}
11 changes: 11 additions & 0 deletions src/Exception/Reflection/MissingPrivatePropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Exception\Reflection;

use Exception;

final class MissingPrivatePropertyException extends Exception
{
}
86 changes: 86 additions & 0 deletions src/Util/ArrayParametersMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Util;

final class ArrayParametersMerger
{
/**
* Merges configurations. Left has higher priority than right one.
*
* @autor David Grudl (https://davidgrudl.com)
* @source https://github.com/nette/di/blob/8eb90721a131262f17663e50aee0032a62d0ef08/src/DI/Config/Helpers.php#L31
*/
public function merge(mixed $left, mixed $right): mixed
{
if (is_array($left) && is_array($right)) {
return $this->mergeLeftToRightWithCallable(
$left,
$right,
fn ($leftValue, $rightValue) => $this->merge($leftValue, $rightValue)
);
}

if ($left !== null) {
return $left;
}

if (! is_array($right)) {
return $left;
}

return $right;
}

/**
* The same as above, just with the case if both values being non-array, it will combined them to array:
*
* $this->mergeWithCombine(1, 2); // [1, 2]
*/
public function mergeWithCombine(mixed $left, mixed $right): mixed
{
if (is_array($left) && is_array($right)) {
return $this->mergeLeftToRightWithCallable(
$left,
$right,
fn ($leftValue, $rightValue) => $this->mergeWithCombine($leftValue, $rightValue)
);
}

if ($left === null && is_array($right)) {
return $right;
}

if (! empty($right) && (array) $left !== (array) $right) {
return $this->mergeWithCombine((array) $right, (array) $left);
}

return $left;
}

/**
* @param array<int|string, mixed> $left
* @param array<int|string, mixed> $right
* @return mixed[]
*/
private function mergeLeftToRightWithCallable(array $left, array $right, callable $mergeCallback): array
{
foreach ($left as $key => $val) {
if (is_int($key)) {
// prevent duplicated values in unindexed arrays
if (! in_array($val, $right, true)) {
$right[] = $val;
}
} else {
if (isset($right[$key])) {
$val = $mergeCallback($val, $right[$key]);
}

$right[$key] = $val;
}
}

return $right;
}
}
4 changes: 2 additions & 2 deletions src/Util/Reflection/PrivatesAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Rector\Core\Util\Reflection;

use Rector\Core\Exception\Reflection\InvalidPrivatePropertyTypeException;
use Rector\Core\Exception\Reflection\MissingPrivatePropertyException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Symplify\PackageBuilder\Exception\InvalidPrivatePropertyTypeException;
use Symplify\PackageBuilder\Exception\MissingPrivatePropertyException;

/**
* @see \Rector\Core\Tests\Util\Reflection\PrivatesAccessorTest
Expand Down

0 comments on commit 6cdebe4

Please sign in to comment.