Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
"composer/xdebug-handler": "^3.0.5",
"doctrine/inflector": "^2.1",
"entropy/entropy": "^0.4.12",
"fidry/cpu-core-counter": "^1.1",
"nette/utils": "^4.1.4",
"nikic/php-parser": "^5.8",
"ondram/ci-detector": "^4.2",
"phpstan/phpdoc-parser": "^2.3.3",
"phpstan/phpstan": "^2.2.6",
"react/child-process": "^0.6.5",
"react/event-loop": "^1.6",
"react/promise": "^3.3",
"react/socket": "^1.17",
Expand All @@ -37,7 +39,6 @@
"symfony/filesystem": "^8.1",
"symfony/finder": "^8.1",
"symfony/process": "^8.1",
"symplify/easy-parallel": "^11.2.2",
"symplify/rule-doc-generator-contracts": "^11.2",
"webmozart/assert": "^2.4"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\FileSystem\FilesFinder;
use Rector\Parallel\Application\ParallelFileProcessor;
use Rector\Parallel\CpuCoreCountProvider;
use Rector\Parallel\Exception\ParallelShouldNotHappenException;
use Rector\Parallel\ScheduleFactory;
use Rector\PhpParser\Parser\ParserErrors;
use Rector\Reporting\MissConfigurationReporter;
use Rector\Skipper\Skipper\UsedSkipCollector;
Expand All @@ -25,9 +28,6 @@
use Rector\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyParallel\CpuCoreCountProvider;
use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyParallel\ScheduleFactory;
use Throwable;

final class ApplicationFileProcessor
Expand Down
2 changes: 1 addition & 1 deletion src/ChangesReporting/ValueObject/RectorWithLineChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\ChangesReporting\ValueObject;

use Rector\Contract\Rector\RectorInterface;
use Rector\Parallel\Contract\SerializableInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Symplify\EasyParallel\Contract\SerializableInterface;
use Webmozart\Assert\Assert;

final readonly class RectorWithLineChange implements SerializableInterface
Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Rector\Configuration\ConfigurationRuleFilter;
use Rector\Configuration\Option;
use Rector\Console\ProcessConfigureDecorator;
use Rector\Parallel\Enum\Action;
use Rector\Parallel\Enum\ReactCommand;
use Rector\Parallel\Enum\ReactEvent;
use Rector\Parallel\ValueObject\Bridge;
use Rector\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Util\MemoryLimiter;
Expand All @@ -23,9 +26,6 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyParallel\Enum\Action;
use Symplify\EasyParallel\Enum\ReactCommand;
use Symplify\EasyParallel\Enum\ReactEvent;
use Throwable;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -154,7 +154,7 @@ private function runWorker(
);

/**
* this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA
* this invokes all listeners listening $decoder->on(...) @see \Rector\Parallel\Enum\ReactEvent::DATA
*/
$encoder->write([
ReactCommand::ACTION => Action::RESULT,
Expand Down
14 changes: 7 additions & 7 deletions src/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Console\Command\ProcessCommand;
use Rector\Parallel\Command\WorkerCommandLineFactory;
use Rector\Parallel\Enum\Action;
use Rector\Parallel\Enum\Content;
use Rector\Parallel\Enum\ReactCommand;
use Rector\Parallel\Enum\ReactEvent;
use Rector\Parallel\ValueObject\Bridge;
use Rector\Parallel\ValueObject\ParallelProcess;
use Rector\Parallel\ValueObject\ProcessPool;
use Rector\Parallel\ValueObject\Schedule;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
use Rector\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyParallel\Enum\Action;
use Symplify\EasyParallel\Enum\Content;
use Symplify\EasyParallel\Enum\ReactCommand;
use Symplify\EasyParallel\Enum\ReactEvent;
use Symplify\EasyParallel\ValueObject\ParallelProcess;
use Symplify\EasyParallel\ValueObject\ProcessPool;
use Symplify\EasyParallel\ValueObject\Schedule;
use Throwable;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Parallel/Command/WorkerCommandLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Configuration\Option;
use Rector\FileSystem\FilePathHelper;
use Rector\Parallel\Exception\ParallelShouldNotHappenException;
use Rector\Parallel\Reflection\CommandFromReflectionFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyParallel\Reflection\CommandFromReflectionFactory;

/**
* @see \Rector\Tests\Parallel\Command\WorkerCommandLineFactoryTest
Expand Down
15 changes: 15 additions & 0 deletions src/Parallel/Contract/SerializableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Contract;

use JsonSerializable;

interface SerializableInterface extends JsonSerializable
{
/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): self;
}
22 changes: 22 additions & 0 deletions src/Parallel/CpuCoreCountProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel;

use Fidry\CpuCoreCounter\CpuCoreCounter;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;

final class CpuCoreCountProvider
{
private const int DEFAULT_CORE_COUNT = 2;

public function provide(): int
{
try {
return new CpuCoreCounter()->getCount();
} catch (NumberOfCpuCoreNotFound) {
return self::DEFAULT_CORE_COUNT;
}
}
}
14 changes: 14 additions & 0 deletions src/Parallel/Enum/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Enum;

final class Action
{
public const string HELLO = 'hello';

public const string MAIN = 'main';

public const string RESULT = 'result';
}
12 changes: 12 additions & 0 deletions src/Parallel/Enum/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Enum;

final class Content
{
public const string RESULT = 'result';

public const string FILES = 'files';
}
12 changes: 12 additions & 0 deletions src/Parallel/Enum/ReactCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Enum;

final class ReactCommand
{
public const string ACTION = 'action';

public const string IDENTIFIER = 'identifier';
}
16 changes: 16 additions & 0 deletions src/Parallel/Enum/ReactEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Enum;

final class ReactEvent
{
public const string EXIT = 'exit';

public const string DATA = 'data';

public const string ERROR = 'error';

public const string CONNECTION = 'connection';
}
11 changes: 11 additions & 0 deletions src/Parallel/Exception/ParallelShouldNotHappenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Exception;

use Exception;

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

declare(strict_types=1);

namespace Rector\Parallel\Reflection;

use Rector\Parallel\Exception\ParallelShouldNotHappenException;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\Console\Command\Command;

final class CommandFromReflectionFactory
{
/**
* @param class-string<Command> $className
*/
public function create(string $className): Command
{
$commandReflectionClass = new ReflectionClass($className);

$command = $commandReflectionClass->newInstanceWithoutConstructor();
$parentClassReflection = $commandReflectionClass->getParentClass();

if (! $parentClassReflection instanceof ReflectionClass) {
throw new ParallelShouldNotHappenException();
}

$parentConstructorReflectionMethod = $parentClassReflection->getConstructor();
if (! $parentConstructorReflectionMethod instanceof ReflectionMethod) {
throw new ParallelShouldNotHappenException();
}

$parentConstructorReflectionMethod->invoke($command);

return $command;
}
}
30 changes: 30 additions & 0 deletions src/Parallel/ScheduleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel;

use Rector\Parallel\ValueObject\Schedule;
use Webmozart\Assert\Assert;

/**
* Used from
* https://github.com/phpstan/phpstan-src/blob/9124c66dcc55a222e21b1717ba5f60771f7dda92/src/Parallel/Scheduler.php
*/
final class ScheduleFactory
{
/**
* @param array<string> $files
*/
public function create(int $cpuCores, int $jobSize, int $maxNumberOfProcesses, array $files): Schedule
{
Assert::positiveInteger($jobSize);

$jobs = array_chunk($files, $jobSize);
$numberOfProcesses = min(count($jobs), $cpuCores);

$numberOfProcesses = min($maxNumberOfProcesses, $numberOfProcesses);

return new Schedule($numberOfProcesses, $jobs);
}
}
Loading
Loading