Skip to content

Commit

Permalink
Require non-capturing catch
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 18, 2021
1 parent f8c1bef commit 89af4e7
Show file tree
Hide file tree
Showing 37 changed files with 67 additions and 57 deletions.
2 changes: 2 additions & 0 deletions build/rector-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector;
use Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector;
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand Down Expand Up @@ -36,6 +37,7 @@

if ($targetPhpVersionId < 80000) {
$services->set(DowngradeTrailingCommasInParamUseRector::class);
$services->set(DowngradeNonCapturingCatchesRector::class);
}

if ($targetPhpVersionId < 70400) {
Expand Down
5 changes: 5 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
<property name="enable" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch">
<properties>
<property name="enable" value="true"/>
</properties>
</rule>
<exclude-pattern>tests/*/data</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_1.php</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_2.php</exclude-pattern>
Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ public function analyseFile(
if ($dependencies->getExportedNode() !== null) {
$exportedNodes[] = $dependencies->getExportedNode();
}
} catch (AnalysedCodeException $e) {
} catch (AnalysedCodeException) {
// pass
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
// pass
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection) {
// pass
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3866,7 +3866,7 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require

try {
$expr = $this->parser->parseString('<?php ' . $exprString . ';')[0];
} catch (ParserErrorsException $e) {
} catch (ParserErrorsException) {
continue;
}
if (!$expr instanceof Node\Stmt\Expression) {
Expand Down Expand Up @@ -3928,7 +3928,7 @@ public function invalidateMethodsOnExpression(Expr $expressionToInvalidate): sel

try {
$expr = $this->parser->parseString('<?php ' . $exprString . ';')[0];
} catch (ParserErrorsException $e) {
} catch (ParserErrorsException) {
continue;
}
if (!$expr instanceof Node\Stmt\Expression) {
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ private function getPhpStanVersion(): string
{
try {
return PrettyVersions::getVersion('phpstan/phpstan')->getPrettyVersion();
} catch (OutOfBoundsException $e) {
} catch (OutOfBoundsException) {
return 'Version unknown';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} elseif ($ci->getCiName() === CiDetector::CI_TEAMCITY) {
$errorFormat = 'teamcity';
}
} catch (CiNotDetectedException $e) {
} catch (CiNotDetectedException) {
// pass
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ClearResultCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
false,
false,
);
} catch (InceptionNotSuccessfulException $e) {
} catch (InceptionNotSuccessfulException) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static function begin(
$memoryLimitFileContents = FileReader::read($memoryLimitFile);
$errorOutput->writeLineFormatted(sprintf('It consumed around %s of memory.', $memoryLimitFileContents));
$errorOutput->writeLineFormatted('');
} catch (CouldNotReadFileException $e) {
} catch (CouldNotReadFileException) {
// pass
}

Expand Down
15 changes: 9 additions & 6 deletions src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function log(string $message): void

try {
$fixerProcess = $this->getFixerProcess($output, $serverPort);
} catch (FixerProcessException $e) {
} catch (FixerProcessException) {
return 1;
}

Expand Down Expand Up @@ -318,7 +318,7 @@ private function getFixerProcess(OutputInterface $output, int $serverPort): Proc

try {
$phar = new Phar($pharPath);
} catch (Throwable $e) {
} catch (Throwable) {
@unlink($pharPath);
@unlink($infoPath);
$output->writeln('<fg=red>PHPStan Pro PHAR signature is corrupted.</>');
Expand Down Expand Up @@ -407,8 +407,11 @@ private function downloadPhar(
),
);

/** @var array{url: string, version: string} $latestInfo */
$latestInfo = Json::decode((string) await($client->get('https://fixer-download-api.phpstan.com/latest'), $loop, 5.0)->getBody(), Json::FORCE_ARRAY); // @phpstan-ignore-line
/**
* @var array{url: string, version: string} $latestInfo
* @phpstan-ignore-next-line
*/
$latestInfo = Json::decode((string) await($client->get('https://fixer-download-api.phpstan.com/latest'), $loop, 5.0)->getBody(), Json::FORCE_ARRAY);
if ($currentVersion !== null && $latestInfo['version'] === $currentVersion) {
$this->writeInfoFile($infoPath, $latestInfo['version']);
$output->writeln('<fg=green>You\'re running the latest PHPStan Pro!</>');
Expand Down Expand Up @@ -586,7 +589,7 @@ private function getPhpstanVersion(): string
{
try {
return PrettyVersions::getVersion('phpstan/phpstan')->getPrettyVersion();
} catch (OutOfBoundsException $e) {
} catch (OutOfBoundsException) {
return 'Version unknown';
}
}
Expand All @@ -601,7 +604,7 @@ private function isDockerRunning(): bool
$contents = FileReader::read('/proc/1/cgroup');

return strpos($contents, 'docker') !== false;
} catch (CouldNotReadFileException $e) {
} catch (CouldNotReadFileException) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/FixerWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$insteadOfFile,
false,
);
} catch (InceptionNotSuccessfulException $e) {
} catch (InceptionNotSuccessfulException) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/IgnoredRegexValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function getIgnoredTypes(TreeNode $ast): array

try {
$type = $this->typeStringResolver->resolve($matches[1], null);
} catch (ParserException $e) {
} catch (ParserException) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
if ($functionName instanceof Node\Name) {
try {
$dependenciesReflections[] = $this->getFunctionReflection($functionName, $scope);
} catch (FunctionNotFoundException $e) {
} catch (FunctionNotFoundException) {
// pass
}
} else {
Expand Down Expand Up @@ -224,7 +224,7 @@ private function addClassToDependencies(string $className, array &$dependenciesR
{
try {
$classReflection = $this->reflectionProvider->getClass($className);
} catch (ClassNotFoundException $e) {
} catch (ClassNotFoundException) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dependency/ExportedNodeFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function fetchNodes(string $fileName): array
try {
/** @var Node[] $ast */
$ast = $this->parser->parseFile($fileName);
} catch (ParserErrorsException $e) {
} catch (ParserErrorsException) {
return [];
}
$this->visitor->reset($fileName);
Expand Down
2 changes: 1 addition & 1 deletion src/Php/PhpVersionFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function create(): PhpVersionFactory
if (is_string($platformVersion)) {
$composerPhpVersion = $platformVersion;
}
} catch (CouldNotReadFileException | JsonException $e) {
} catch (CouldNotReadFileException | JsonException) {
// pass
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public function hasClass(string $className): bool
try {
$this->reflector->reflectClass($className);
return true;
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
return false;
} catch (InvalidIdentifierName $e) {
} catch (InvalidIdentifierName) {
return false;
}
}
Expand All @@ -154,7 +154,7 @@ public function getClass(string $className): ClassReflection

try {
$reflectionClass = $this->reflector->reflectClass($className);
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
throw new ClassNotFoundException($className);
}

Expand Down Expand Up @@ -336,9 +336,9 @@ public function resolveFunctionName(Node\Name $nameNode, ?Scope $scope): ?string
try {
$this->reflector->reflectFunction($name);
return true;
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
// pass
} catch (InvalidIdentifierName $e) {
} catch (InvalidIdentifierName) {
// pass
}

Expand Down Expand Up @@ -370,7 +370,7 @@ public function getConstant(Node\Name $nameNode, ?Scope $scope): GlobalConstantR
$constantValue = $constantReflection->getValue();
$constantValueType = ConstantTypeHelper::getTypeFromValue($constantValue);
$fileName = $constantReflection->getFileName();
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection) {
$constantValueType = new MixedType();
$fileName = null;
}
Expand All @@ -388,9 +388,9 @@ public function resolveConstantName(Node\Name $nameNode, ?Scope $scope): ?string
try {
$this->reflector->reflectConstant($name);
return true;
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
// pass
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $e) {
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection) {
// pass
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function enterNode(Node $node): ?int
if ($node instanceof Node\Expr\FuncCall) {
try {
ConstantNodeChecker::assertValidDefineFunctionCall($node);
} catch (InvalidConstantNode $e) {
} catch (InvalidConstantNode) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public function create(string $projectInstallationPath): ?SourceLocator
try {
$composerJsonContents = FileReader::read($composerJsonPath);
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
} catch (CouldNotReadFileException | JsonException $e) {
} catch (CouldNotReadFileException | JsonException) {
return null;
}

try {
$installedJsonContents = FileReader::read($installedJsonPath);
$installedJson = Json::decode($installedJsonContents, Json::FORCE_ARRAY);
} catch (CouldNotReadFileException | JsonException $e) {
} catch (CouldNotReadFileException | JsonException) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function fetchNodes(string $fileName): FetchedNodesResult
try {
/** @var Node[] $ast */
$ast = $this->parser->parseFile($fileName);
} catch (ParserErrorsException $e) {
} catch (ParserErrorsException) {
return new FetchedNodesResult([], [], []);
}
$this->cachingVisitor->reset($fileName, $contents);
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/ClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getValue()
{
try {
return $this->reflection->getValue();
} catch (UnableToCompileNode $e) {
} catch (UnableToCompileNode) {
return NAN;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public function isSubclassOf(string $className): bool

try {
return $this->subclasses[$className] = $this->reflection->isSubclassOf($className);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
return $this->subclasses[$className] = false;
}
}
Expand All @@ -580,7 +580,7 @@ public function implementsInterface(string $className): bool
{
try {
return $this->reflection->implementsInterface($className);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
return false;
}
}
Expand Down Expand Up @@ -884,7 +884,7 @@ public function getTypeAliases(): array

try {
$typeAliases = $importedFromReflection->getTypeAliases();
} catch (CircularTypeAliasDefinitionException $e) {
} catch (CircularTypeAliasDefinitionException) {
return TypeAlias::invalid();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getPrototype(): ClassMemberReflection
$prototypeDeclaringClass->getNativeMethod($prototypeMethod->getName())->getVariants(),
$tentativeReturnType,
);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
return $this;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Php/PhpMethodFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getPrototype(): ClassMemberReflection
{
try {
return $this->declaringClass->getNativeMethod($this->getClassMethod()->name->name)->getPrototype();
} catch (MissingMethodFromReflectionException $e) {
} catch (MissingMethodFromReflectionException) {
return $this;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function getPrototype(): ClassMemberReflection
$prototypeDeclaringClass->getNativeMethod($prototypeMethod->getName())->getVariants(),
$tentativeReturnType,
);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
return $this;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Php/PhpParameterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getType(): Type
if ($this->reflection->isDefaultValueAvailable() && $this->reflection->getDefaultValue() === null) {
$phpDocType = TypeCombinator::addNull($phpDocType);
}
} catch (Throwable $e) {
} catch (Throwable) {
// pass
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public function getDefaultValue(): ?Type
$defaultValue = $this->reflection->getDefaultValue();
return ConstantTypeHelper::getTypeFromValue($defaultValue);
}
} catch (Throwable $e) {
} catch (Throwable) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
}
$isDeprecated = $reflectionFunction->isDeprecated();
}
} catch (IdentifierNotFound $e) {
} catch (IdentifierNotFound) {
// pass
} catch (InvalidIdentifierName $e) {
} catch (InvalidIdentifierName) {
// pass
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Api/PhpStanNamespaceIn3rdPartyPackageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ private function findComposerJsonContents(string $fromDirectory): ?array

try {
return Json::decode(FileReader::read($composerJsonPath), Json::FORCE_ARRAY);
} catch (JsonException $e) {
} catch (JsonException) {
return null;
} catch (CouldNotReadFileException $e) {
} catch (CouldNotReadFileException) {
return null;
}
}
Expand Down
Loading

0 comments on commit 89af4e7

Please sign in to comment.