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

voku / Simple-PHP-Code-Parser / 24272981784

11 Apr 2026 02:51AM UTC coverage: 79.752% (-3.5%) from 83.27%
24272981784

Pull #78

github

web-flow
Merge 7f69b0ffc into cc07ae5a0
Pull Request #78: Update dependencies: php-parser v5, phpdoc-parser v2, reflection-docblock v6, type-resolver v2, phpunit v11

26 of 33 new or added lines in 9 files covered. (78.79%)

27 existing lines in 3 files now uncovered.

1481 of 1857 relevant lines covered (79.75%)

33.51 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);
41✔
53

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

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

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

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

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

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

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

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

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

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

93
        return $this;
41✔
94
    }
95

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

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

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

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

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

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

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

134
        return $this;
53✔
135
    }
136

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

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

152
        return $namespace . $nodeName;
44✔
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