Skip to content

Commit

Permalink
[TypeDeclaration] Skip Conditional on ReturnNeverTypeRector (#1200)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <markus.staab@redaxo.de>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Nov 10, 2021
1 parent d9ef4fb commit f32035d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

function imagesavetofile($gdImage, $filename, $quality)
{
$ext = pathinfo($filename, \PATHINFO_EXTENSION);
if (!$ext) {
throw new Exception('Unable to determine file extension for '.$filename);
}

switch (strtolower($ext)) {
case 'jpeg':
case 'jpg':
imagejpeg($gdImage, $filename, $quality);
break;

case 'png':
imagepng($gdImage, $filename, $pngQuality);

break;

case 'gif':
imagegif($gdImage, $filename);
break;

default:
throw new Exception('Unsupported file extension '.$ext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

class XY
{
private function myMethod($string)
{
$colParts = preg_split('/\s+/', $string, null, \PREG_SPLIT_NO_EMPTY);
$numParts = count($colParts);

if (3 == $numParts) {
} elseif (2 == $numParts) {
} elseif (1 == $numParts) {
if ('*' != $colParts[0]) {
$this->string($colParts[0]);
}
} else {
throw new Exception(sprintf('"%s" is not valid'));
}
}
}
16 changes: 10 additions & 6 deletions rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNestingScope\ValueObject\ControlStructure;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -96,20 +97,23 @@ public function refactor(Node $node): ?Node

private function shouldSkip(ClassMethod | Function_ $node): bool
{
$returns = $this->betterNodeFinder->findInstanceOf($node, Return_::class);
if ($returns !== []) {
$return = $this->betterNodeFinder->findFirstInstanceOf($node, Return_::class);
if ($return instanceof Return_) {
return true;
}

$notNeverNodes = $this->betterNodeFinder->findInstanceOf($node, Yield_::class);
if ($notNeverNodes !== []) {
$hasNotNeverNodes = $this->betterNodeFinder->hasInstancesOf(
$node,
[Yield_::class] + ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES
);
if ($hasNotNeverNodes) {
return true;
}

$neverNodes = $this->betterNodeFinder->findInstancesOf($node, [Node\Expr\Throw_::class, Throw_::class]);
$hasNeverNodes = $this->betterNodeFinder->hasInstancesOf($node, [Node\Expr\Throw_::class, Throw_::class]);

$hasNeverFuncCall = $this->hasNeverFuncCall($node);
if ($neverNodes === [] && ! $hasNeverFuncCall) {
if (! $hasNeverNodes && ! $hasNeverFuncCall) {
return true;
}

Expand Down

0 comments on commit f32035d

Please sign in to comment.