Skip to content

Commit

Permalink
Show different code on found errors, and on fatal errors (#3297)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 20, 2023
1 parent 1ab342a commit 6ff4617
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
37 changes: 20 additions & 17 deletions rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php82\Rector\New_;

use FilesystemIterator;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand All @@ -21,22 +22,20 @@
/**
* @see \Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\FilesystemIteratorSkipDotsRectorTest
*/
class FilesystemIteratorSkipDotsRector extends AbstractRector implements MinPhpVersionInterface
final class FilesystemIteratorSkipDotsRector extends AbstractRector implements MinPhpVersionInterface
{
private ClassConstFetchNameResolver $classConstFetchNameResolver;

public function __construct(ClassConstFetchNameResolver $classConstFetchNameResolver)
{
$this->classConstFetchNameResolver = $classConstFetchNameResolver;
public function __construct(
private readonly ClassConstFetchNameResolver $classConstFetchNameResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Prior PHP 8.2 FilesystemIterator::SKIP_DOTS was always set and could not be removed, therefore FilesystemIterator::SKIP_DOTS is added in order to keep this behaviour.',
[new CodeSample(
'new \\FilesystemIterator(__DIR__, \\FilesystemIterator::KEY_AS_FILENAME);',
'new \\FilesystemIterator(__DIR__, \\FilesystemIterator::KEY_AS_FILENAME | \\FilesystemIterator::SKIP_DOTS);'
'new ' . FilesystemIterator::class . '(__DIR__, ' . FilesystemIterator::class . '::KEY_AS_FILENAME);',
'new ' . FilesystemIterator::class . '(__DIR__, ' . FilesystemIterator::class . '::KEY_AS_FILENAME | ' . FilesystemIterator::class . '::SKIP_DOTS);'
),
]
);
Expand All @@ -57,15 +56,18 @@ public function refactor(Node $node): ?New_
if ($node->isFirstClassCallable()) {
return null;
}
if (!array_key_exists(1, $node->args)) {

if (! array_key_exists(1, $node->args)) {
return null;
}

$flags = $node->args[1]->value;
if ($this->isSkipDotsPresent($flags)) {
return null;
}
$skipDots = new ClassConstFetch(new FullyQualified('FilesystemIterator'), 'SKIP_DOTS');
$node->args[1] = new Arg(new BitwiseOr($flags, $skipDots));

$classConstFetch = new ClassConstFetch(new FullyQualified('FilesystemIterator'), 'SKIP_DOTS');
$node->args[1] = new Arg(new BitwiseOr($flags, $classConstFetch));
return $node;
}

Expand All @@ -77,24 +79,25 @@ public function provideMinPhpVersion(): int
/**
* Is the constant {@see \FilesystemIterator::SKIP_DOTS} present within $node?
*/
private function isSkipDotsPresent(Expr $node): bool
private function isSkipDotsPresent(Expr $expr): bool
{
while ($node instanceof BitwiseOr) {
if ($this->isSkipDots($node->right)) {
while ($expr instanceof BitwiseOr) {
if ($this->isSkipDots($expr->right)) {
return true;
}
$node = $node->left;

$expr = $expr->left;
}

return $this->isSkipDots($node);
return $this->isSkipDots($expr);
}

/**
* Tells if $expr is equal to {@see \FilesystemIterator::SKIP_DOTS}.
*/
private function isSkipDots(Expr $expr): bool
{
if (!$expr instanceof ClassConstFetch) {
if (! $expr instanceof ClassConstFetch) {
return false;
}

Expand Down
15 changes: 11 additions & 4 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Autoloading\AdditionalAutoloader;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\ExitCode;
use Rector\Core\Console\Output\OutputFormatterCollector;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Exception\ShouldNotHappenException;
Expand All @@ -20,7 +21,6 @@
use Rector\Core\ValueObject\ProcessResult;
use Rector\Core\ValueObjectFactory\ProcessResultFactory;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -129,18 +129,25 @@ private function invalidateCacheForChangedAndErroredFiles(ProcessResult $process
}
}

/**
* @return ExitCode::*
*/
private function resolveReturnCode(ProcessResult $processResult, Configuration $configuration): int
{
// some system errors were found → fail
if ($processResult->getErrors() !== []) {
return Command::FAILURE;
return ExitCode::FAILURE;
}

// inverse error code for CI dry-run
if (! $configuration->isDryRun()) {
return Command::SUCCESS;
return ExitCode::SUCCESS;
}

return $processResult->getFileDiffs() === [] ? Command::SUCCESS : Command::FAILURE;
if ($processResult->getFileDiffs() !== []) {
return ExitCode::CHANGED_CODE;
}

return ExitCode::SUCCESS;
}
}
28 changes: 28 additions & 0 deletions src/Console/ExitCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Console;

use Symfony\Component\Console\Command\Command;

/**
* @api
*/
final class ExitCode
{
/**
* @var int
*/
public const SUCCESS = Command::SUCCESS;

/**
* @var int
*/
public const FAILURE = Command::FAILURE;

/**
* @var int
*/
public const CHANGED_CODE = 2;
}

0 comments on commit 6ff4617

Please sign in to comment.