• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / Simple-PHP-Code-Parser / 24264577593

10 Apr 2026 09:16PM UTC coverage: 83.27%. Remained the same
24264577593

Pull #81

github

web-flow
Merge c3f94c748 into cc07ae5a0
Pull Request #81: Update actions/upload-artifact action to v7.0.1

1543 of 1853 relevant lines covered (83.27%)

43.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.83
/src/voku/SimplePhpParser/Model/PHPConst.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace voku\SimplePhpParser\Model;
6

7
use PhpParser\Node\Const_;
8
use PhpParser\Node\Name;
9
use PhpParser\Node\Stmt\ClassConst;
10
use PhpParser\Node\Stmt\Namespace_;
11
use PhpParser\NodeAbstract;
12
use ReflectionClassConstant;
13
use voku\SimplePhpParser\Parsers\Helper\Utils;
14

15
class PHPConst extends BasePHPElement
16
{
17
    use PHPDocElement;
18

19
    public ?string $parentName = null;
20

21
    /**
22
     * @var array|bool|float|int|string|null
23
     *
24
     * @phpstan-var scalar|array<scalar>|null
25
     */
26
    public $value;
27

28
    public ?string $visibility = null;
29

30
    public ?string $type = null;
31

32
    /**
33
     * Type from the constant's native type declaration (PHP 8.3+).
34
     */
35
    public ?string $typeFromDeclaration = null;
36

37
    /**
38
     * PHP 8.0+ attributes on this constant.
39
     *
40
     * @var PHPAttribute[]
41
     */
42
    public array $attributes = [];
43

44
    /**
45
     * @param Const_ $node
46
     * @param null   $dummy
47
     *
48
     * @return $this
49
     */
50
    public function readObjectFromPhpNode($node, $dummy = null): self
51
    {
52
        $this->prepareNode($node);
53✔
53

54
        $this->name = $this->getConstantFQN($node, $node->name->name);
53✔
55

56
        $this->value = Utils::getPhpParserValueFromNode($node);
53✔
57

58
        $this->type = Utils::normalizePhpType(\gettype($this->value));
53✔
59

60
        $parentNode = $node->getAttribute('parent');
53✔
61

62
        if ($parentNode instanceof ClassConst) {
53✔
63
            if ($parentNode->isPrivate()) {
53✔
64
                $this->visibility = 'private';
14✔
65
            } elseif ($parentNode->isProtected()) {
53✔
66
                $this->visibility = 'protected';
13✔
67
            } else {
68
                $this->visibility = 'public';
53✔
69
            }
70

71
            $this->parentName = self::getFQN($parentNode->getAttribute('parent'));
53✔
72

73
            // Typed class constants (PHP 8.3+)
74
            if (\property_exists($parentNode, 'type') && $parentNode->type !== null) {
53✔
75
                $typeDeclStr = Utils::typeNodeToString($parentNode->type);
10✔
76
                if ($typeDeclStr !== null) {
10✔
77
                    $this->typeFromDeclaration = $typeDeclStr;
10✔
78
                }
79
            }
80

81
            // Extract PHP 8.0+ attributes (only if not already populated by reflection)
82
            if (empty($this->attributes) && !empty($parentNode->attrGroups)) {
53✔
83
                $this->attributes = Utils::extractAttributesFromAstNode($parentNode->attrGroups);
×
84
            }
85
        }
86

87
        $this->collectTags($node);
53✔
88

89
        if ($node->getAttribute('parent') instanceof ClassConst) {
53✔
90
            $this->parentName = static::getFQN($node->getAttribute('parent')->getAttribute('parent'));
53✔
91
        }
92

93
        return $this;
53✔
94
    }
95

96
    /**
97
     * @param ReflectionClassConstant $constant
98
     *
99
     * @return $this
100
     */
101
    public function readObjectFromReflection($constant): self
102
    {
103
        $this->name = $constant->getName();
69✔
104

105
        $file = $constant->getDeclaringClass()->getFileName();
69✔
106
        if ($file) {
69✔
107
            $this->file = $file;
69✔
108
        }
109

110
        /** @psalm-suppress InvalidPropertyAssignmentValue - upstream phpdoc error ? */
111
        $this->value = $constant->getValue();
69✔
112

113
        $this->type = \gettype($this->value);
69✔
114

115
        if ($constant->isPrivate()) {
69✔
116
            $this->visibility = 'private';
14✔
117
        } elseif ($constant->isProtected()) {
69✔
118
            $this->visibility = 'protected';
11✔
119
        } else {
120
            $this->visibility = 'public';
69✔
121
        }
122

123
        // Typed class constants (PHP 8.3+)
124
        if (\method_exists($constant, 'getType')) {
69✔
125
            $reflType = $constant->getType();
36✔
126
            if ($reflType !== null) {
36✔
127
                $this->typeFromDeclaration = (string) $reflType;
6✔
128
            }
129
        }
130

131
        // Extract PHP 8.0+ attributes
132
        $this->attributes = Utils::extractAttributesFromReflection($constant);
69✔
133

134
        return $this;
69✔
135
    }
136

137
    protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
138
    {
139
        $parent = $node->getAttribute('parent');
57✔
140
        $parentParentNode = $parent ? $parent->getAttribute('parent') : null;
57✔
141

142
        if (
143
            $parentParentNode instanceof Namespace_
57✔
144
            &&
145
            $parentParentNode->name instanceof Name
57✔
146
        ) {
147
            $namespace = '\\' . $parentParentNode->name->toString() . '\\';
16✔
148
        } else {
149
            $namespace = '';
57✔
150
        }
151

152
        return $namespace . $nodeName;
57✔
153
    }
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc