Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php declare(strict_types=1); namespace Larastan\Larastan\Types; use Illuminate\Databa..

Decoded Output download

<?php

declare(strict_types=1);

namespace Larastan\Larastan\Types;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\TypeNodeResolverExtension;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

use function count;

class GenericEloquentBuilderTypeNodeResolverExtension implements TypeNodeResolverExtension
{
    public function __construct(private ReflectionProvider $provider)
    {
    }

    public function resolve(TypeNode $typeNode, NameScope $nameScope): Type|null
    {
        if (! $typeNode instanceof UnionTypeNode || count($typeNode->types) !== 2) {
            return null;
        }

        $modelTypeNode   = null;
        $builderTypeNode = null;
        foreach ($typeNode->types as $innerTypeNode) {
            if (
                ! ($innerTypeNode instanceof IdentifierTypeNode)
                || ! $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
                || ! (new ObjectType(Model::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes()
            ) {
                continue;
            }

            $modelTypeNode = $innerTypeNode;
        }

        if ($modelTypeNode === null) {
            return null;
        }

        foreach ($typeNode->types as $innerTypeNode) {
            if (
                ! ($innerTypeNode instanceof IdentifierTypeNode)
                || ! $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
                || ($nameScope->resolveStringName($innerTypeNode->name) !== Builder::class && ! (new ObjectType(Builder::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes())
            ) {
                continue;
            }

            $builderTypeNode = $innerTypeNode;
        }

        if ($builderTypeNode === null) {
            return null;
        }

        $builderTypeName = $nameScope->resolveStringName($builderTypeNode->name);
        $modelTypeName   = $nameScope->resolveStringName($modelTypeNode->name);

        return new GenericObjectType($builderTypeName, [
            new ObjectType($modelTypeName),
        ]);
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php

declare(strict_types=1);

namespace Larastan\Larastan\Types;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\TypeNodeResolverExtension;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

use function count;

class GenericEloquentBuilderTypeNodeResolverExtension implements TypeNodeResolverExtension
{
    public function __construct(private ReflectionProvider $provider)
    {
    }

    public function resolve(TypeNode $typeNode, NameScope $nameScope): Type|null
    {
        if (! $typeNode instanceof UnionTypeNode || count($typeNode->types) !== 2) {
            return null;
        }

        $modelTypeNode   = null;
        $builderTypeNode = null;
        foreach ($typeNode->types as $innerTypeNode) {
            if (
                ! ($innerTypeNode instanceof IdentifierTypeNode)
                || ! $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
                || ! (new ObjectType(Model::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes()
            ) {
                continue;
            }

            $modelTypeNode = $innerTypeNode;
        }

        if ($modelTypeNode === null) {
            return null;
        }

        foreach ($typeNode->types as $innerTypeNode) {
            if (
                ! ($innerTypeNode instanceof IdentifierTypeNode)
                || ! $this->provider->hasClass($nameScope->resolveStringName($innerTypeNode->name))
                || ($nameScope->resolveStringName($innerTypeNode->name) !== Builder::class && ! (new ObjectType(Builder::class))->isSuperTypeOf(new ObjectType($nameScope->resolveStringName($innerTypeNode->name)))->yes())
            ) {
                continue;
            }

            $builderTypeNode = $innerTypeNode;
        }

        if ($builderTypeNode === null) {
            return null;
        }

        $builderTypeName = $nameScope->resolveStringName($builderTypeNode->name);
        $modelTypeName   = $nameScope->resolveStringName($modelTypeNode->name);

        return new GenericObjectType($builderTypeName, [
            new ObjectType($modelTypeName),
        ]);
    }
}

Function Calls

None

Variables

None

Stats

MD5 e12f76de2652315f83e71b9859bce6c7
Eval Count 0
Decode Time 94 ms