PHP 8.2.31
Preview: ExprAnalyzer.php Size: 3.62 KB
/opt/cpanel/ea-wappspector/vendor/rector/rector/src/NodeAnalyzer/ExprAnalyzer.php

<?php

declare (strict_types=1);
namespace Rector\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use Rector\Enum\ObjectReference;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class ExprAnalyzer
{
    public function isNonTypedFromParam(Expr $expr) : bool
    {
        if (!$expr instanceof Variable) {
            return \false;
        }
        $scope = $expr->getAttribute(AttributeKey::SCOPE);
        if (!$scope instanceof Scope) {
            // uncertainty when scope not yet filled/overlapped on just refactored
            return \true;
        }
        $nativeType = $scope->getNativeType($expr);
        if ($nativeType instanceof MixedType && !$nativeType->isExplicitMixed()) {
            return \true;
        }
        $type = $scope->getType($expr);
        if ($nativeType instanceof ObjectWithoutClassType && !$type instanceof ObjectWithoutClassType) {
            return \true;
        }
        if ($nativeType instanceof UnionType) {
            return !$nativeType->equals($type);
        }
        return !$nativeType->isSuperTypeOf($type)->yes();
    }
    public function isDynamicExpr(Expr $expr) : bool
    {
        // Unwrap UnaryPlus and UnaryMinus
        if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) {
            $expr = $expr->expr;
        }
        if ($expr instanceof Array_) {
            return $this->isDynamicArray($expr);
        }
        if ($expr instanceof Scalar) {
            // string interpolation is true, otherwise false
            return $expr instanceof Encapsed;
        }
        return !$this->isAllowedConstFetchOrClassConstFetch($expr);
    }
    public function isDynamicArray(Array_ $array) : bool
    {
        foreach ($array->items as $item) {
            if (!$item instanceof ArrayItem) {
                continue;
            }
            if (!$this->isAllowedArrayKey($item->key)) {
                return \true;
            }
            if (!$this->isAllowedArrayValue($item->value)) {
                return \true;
            }
        }
        return \false;
    }
    private function isAllowedConstFetchOrClassConstFetch(Expr $expr) : bool
    {
        if ($expr instanceof ConstFetch) {
            return \true;
        }
        if ($expr instanceof ClassConstFetch) {
            if (!$expr->class instanceof Name) {
                return \false;
            }
            if (!$expr->name instanceof Identifier) {
                return \false;
            }
            // static::class cannot be used for compile-time class name resolution
            return $expr->class->toString() !== ObjectReference::STATIC;
        }
        return \false;
    }
    private function isAllowedArrayKey(?Expr $expr) : bool
    {
        if (!$expr instanceof Expr) {
            return \true;
        }
        if ($expr instanceof String_) {
            return \true;
        }
        return $expr instanceof LNumber;
    }
    private function isAllowedArrayValue(Expr $expr) : bool
    {
        if ($expr instanceof Array_) {
            return !$this->isDynamicArray($expr);
        }
        return !$this->isDynamicExpr($expr);
    }
}

Directory Contents

Dirs: 0 × Files: 17

Name Size Perms Modified Actions
418 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.14 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.37 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
463 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
2.06 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.02 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.99 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
3.62 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
717 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
5.39 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.66 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
7.38 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
5.91 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
683 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
5.25 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download
603 B lrw-r--r-- 2024-11-08 13:59:10
Edit Download
1.51 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).