Phpstan/phpdoc-trình phân tích cú pháp

Hôm nay chúng ta xem xét cách sửa đổi docblocks một cách dễ dàng. Nhưng trước tiên, chúng ta phải tìm hiểu cách thức hoạt động của

use Symplify\Astral\PhpDocParser\StaticFactory\SimplePhpDocParserStaticFactory;

$values = children as $phpDocChildNode] {
    if [! $phpDocChildNode instanceof PhpDocTagNode] {
        continue;
    }

    // is this node a @return?
    if [! $phpDocChildNode->value instanceof ReturnTagValueNode] {
        continue;
    }

    // does @return have simple type? here can be any TypeNode
    // e.g. UnionTypeNode, IntersectionTypeNode, CallableTypeNode etc.
    $returnTagValueNode = $phpDocChildNode->value;
    if [! $returnTagValueNode->type instanceof IdentifierTypeNode] {
        continue;
    }

    $identifierName = $returnTagValueNode->type->name;
    if [! $identifierName === 'int'] {
        continue;
    }

    // phew.. here we can finally change value
    $returnTagValueNode->type = new IdentifierTypeNode['string'];
}
2 đi qua mọi thuộc tính công khai của nút đó [xem trên Github]

Điều đó có nghĩa là nếu

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;

/** @var PhpDocNode $phpDocNode */
foreach [$phpDocNode->children as $phpDocChildNode] {
    if [! $phpDocChildNode instanceof PhpDocTagNode] {
        continue;
    }

    // is this node a @return?
    if [! $phpDocChildNode->value instanceof ReturnTagValueNode] {
        continue;
    }

    // does @return have simple type? here can be any TypeNode
    // e.g. UnionTypeNode, IntersectionTypeNode, CallableTypeNode etc.
    $returnTagValueNode = $phpDocChildNode->value;
    if [! $returnTagValueNode->type instanceof IdentifierTypeNode] {
        continue;
    }

    $identifierName = $returnTagValueNode->type->name;
    if [! $identifierName === 'int'] {
        continue;
    }

    // phew.. here we can finally change value
    $returnTagValueNode->type = new IdentifierTypeNode['string'];
}
4 đi vào, nó sẽ đi qua đó

composer require symplify/astral
5

Nếu

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;

/** @var PhpDocNode $phpDocNode */
foreach [$phpDocNode->children as $phpDocChildNode] {
    if [! $phpDocChildNode instanceof PhpDocTagNode] {
        continue;
    }

    // is this node a @return?
    if [! $phpDocChildNode->value instanceof ReturnTagValueNode] {
        continue;
    }

    // does @return have simple type? here can be any TypeNode
    // e.g. UnionTypeNode, IntersectionTypeNode, CallableTypeNode etc.
    $returnTagValueNode = $phpDocChildNode->value;
    if [! $returnTagValueNode->type instanceof IdentifierTypeNode] {
        continue;
    }

    $identifierName = $returnTagValueNode->type->name;
    if [! $identifierName === 'int'] {
        continue;
    }

    // phew.. here we can finally change value
    $returnTagValueNode->type = new IdentifierTypeNode['string'];
}
5 là nút, nó sẽ đi qua tất cả các thuộc tính công khai của nó, v.v.


Công khai các thuộc tính là một quy ước trong cả trình phân tích cú pháp phpdoc và trình phân tích cú pháp php, vì vậy chúng tôi có thể tin tưởng 100% vào nó

Chủ Đề