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

voku / Simple-PHP-Code-Parser / 24277438854

11 Apr 2026 07:15AM UTC coverage: 82.757%. Remained the same
24277438854

push

github

web-flow
Merge pull request #85 from voku/renovate/migrate-config

chore(config): migrate Renovate config

1531 of 1850 relevant lines covered (82.76%)

90.77 hits per line

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

97.96
/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);
110✔
53

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

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

58
        $parentNode = $node->getAttribute('parent');
110✔
59

60
        if ($parentNode instanceof ClassConst) {
110✔
61
            if ($parentNode->type !== null) {
110✔
62
                $this->type = Utils::typeNodeToString($parentNode->type);
24✔
63
            }
64

65
            if ($parentNode->isPrivate()) {
110✔
66
                $this->visibility = 'private';
28✔
67
            } elseif ($parentNode->isProtected()) {
110✔
68
                $this->visibility = 'protected';
26✔
69
            } else {
70
                $this->visibility = 'public';
110✔
71
            }
72

73
            $this->parentName = self::getFQN($parentNode->getAttribute('parent'));
110✔
74

75
            // Typed class constants (PHP 8.3+)
76
            if ($parentNode->type !== null) {
110✔
77
                $typeDeclStr = Utils::typeNodeToString($parentNode->type);
24✔
78
                if ($typeDeclStr !== null) {
24✔
79
                    $this->typeFromDeclaration = $typeDeclStr;
24✔
80
                }
81
            }
82

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

89
        if ($this->type === null) {
110✔
90
            $this->type = Utils::normalizePhpType(\gettype($this->value));
102✔
91
        }
92

93
        $this->collectTags($node);
110✔
94

95
        if ($node->getAttribute('parent') instanceof ClassConst) {
110✔
96
            $this->parentName = static::getFQN($node->getAttribute('parent')->getAttribute('parent'));
110✔
97
        }
98

99
        return $this;
110✔
100
    }
101

102
    /**
103
     * @param ReflectionClassConstant $constant
104
     *
105
     * @return $this
106
     */
107
    public function readObjectFromReflection($constant): self
108
    {
109
        $this->name = $constant->getName();
138✔
110

111
        $file = $constant->getDeclaringClass()->getFileName();
138✔
112
        if ($file) {
138✔
113
            $this->file = $file;
138✔
114
        }
115

116
        /** @psalm-suppress InvalidPropertyAssignmentValue - upstream phpdoc error ? */
117
        $this->value = $constant->getValue();
138✔
118

119
        $this->type = \gettype($this->value);
138✔
120

121
        if ($constant->isPrivate()) {
138✔
122
            $this->visibility = 'private';
28✔
123
        } elseif ($constant->isProtected()) {
138✔
124
            $this->visibility = 'protected';
22✔
125
        } else {
126
            $this->visibility = 'public';
138✔
127
        }
128

129
        // Typed class constants (PHP 8.3+)
130
        if (\method_exists($constant, 'getType')) {
138✔
131
            $reflType = $constant->getType();
72✔
132
            if ($reflType !== null) {
72✔
133
                $this->typeFromDeclaration = (string) $reflType;
12✔
134
            }
135
        }
136

137
        // Extract PHP 8.0+ attributes
138
        $this->attributes = Utils::extractAttributesFromReflection($constant);
138✔
139

140
        return $this;
138✔
141
    }
142

143
    protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
144
    {
145
        $parent = $node->getAttribute('parent');
118✔
146
        $parentParentNode = $parent ? $parent->getAttribute('parent') : null;
118✔
147

148
        if (
149
            $parentParentNode instanceof Namespace_
118✔
150
            &&
151
            $parentParentNode->name instanceof Name
118✔
152
        ) {
153
            $namespace = '\\' . $parentParentNode->name->toString() . '\\';
32✔
154
        } else {
155
            $namespace = '';
118✔
156
        }
157

158
        return $namespace . $nodeName;
118✔
159
    }
160
}
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