Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRector\Fixture;

use Monolog\Logger;
use Monolog\LogRecord;

final class SkipConstantCompareFromVendor
{
public function createLogRecord(int $level): array|LogRecord
{
return Logger::API === 2
? ['level' => $level]
: new LogRecord();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
Expand All @@ -19,6 +21,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType as PHPStanUnionType;
Expand All @@ -30,6 +33,7 @@
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Skipper\FileSystem\PathNormalizer;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -255,12 +259,55 @@ private function collectActualReturnTypes(array $returnStatements): array
continue;
}

$returnTypes[] = $this->nodeTypeResolver->getNativeType($returnStatement->expr);
$returnTypes = [...$returnTypes, ...$this->resolveNativeReturnTypes($returnStatement->expr)];
}

return $returnTypes;
}

/**
* @return Type[]
*/
private function resolveNativeReturnTypes(Expr $expr): array
{
if (! $expr instanceof Ternary || ! $this->hasVendorClassConstFetch($expr->cond)) {
return [$this->nodeTypeResolver->getNativeType($expr)];
}

$ifExpr = $expr->if instanceof Expr ? $expr->if : $expr->cond;

return [...$this->resolveNativeReturnTypes($ifExpr), ...$this->resolveNativeReturnTypes($expr->else)];
}

private function hasVendorClassConstFetch(Expr $expr): bool
{
$classConstFetches = $this->betterNodeFinder->findInstanceOf($expr, ClassConstFetch::class);

foreach ($classConstFetches as $classConstFetch) {
$classType = $this->nodeTypeResolver->getType($classConstFetch->class);
if (! $classType instanceof ObjectType) {
continue;
}

$classReflection = $classType->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
continue;
}

$fileName = $classReflection->getFileName();
if ($fileName === null) {
continue;
}

$normalizedFileName = PathNormalizer::normalize($fileName);
if (str_contains($normalizedFileName, '/vendor/')) {
return true;
}
}

return false;
Comment thread
TomasVotruba marked this conversation as resolved.
}

/**
* @param Type[] $actualReturnTypes
* @return Type[]
Expand Down
13 changes: 13 additions & 0 deletions stubs/Monolog/vendor/monolog/LogRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Monolog;

if (class_exists('Monolog\LogRecord')) {
return;
}

class LogRecord
{
}
14 changes: 14 additions & 0 deletions stubs/Monolog/vendor/monolog/v2/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Monolog;

if (class_exists('Monolog\Logger')) {
return;
}

class Logger
{
public const API = 2;
}
14 changes: 14 additions & 0 deletions stubs/Monolog/vendor/monolog/v3/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Monolog;

if (class_exists('Monolog\Logger')) {
return;
}

class Logger
{
public const API = 3;
}
Loading