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

voku / Simple-PHP-Code-Parser / 12186673668

05 Dec 2024 07:26PM UTC coverage: 79.926% (-2.3%) from 82.259%
12186673668

Pull #54

github

web-flow
Merge 30436eb42 into 898f2c327
Pull Request #54: Update actions/cache action to v4

1302 of 1629 relevant lines covered (79.93%)

6.18 hits per line

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

91.43
/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
     * @param Const_ $node
34
     * @param null   $dummy
35
     *
36
     * @return $this
37
     */
38
    public function readObjectFromPhpNode($node, $dummy = null): self
39
    {
40
        $this->prepareNode($node);
6✔
41

42
        $this->name = $this->getConstantFQN($node, $node->name->name);
6✔
43

44
        $this->value = Utils::getPhpParserValueFromNode($node);
6✔
45

46
        $this->type = Utils::normalizePhpType(\gettype($this->value));
6✔
47

48
        $parentNode = $node->getAttribute('parent');
6✔
49

50
        if ($parentNode instanceof ClassConst) {
6✔
51
            if ($parentNode->isPrivate()) {
6✔
52
                $this->visibility = 'private';
×
53
            } elseif ($parentNode->isProtected()) {
6✔
54
                $this->visibility = 'protected';
×
55
            } else {
56
                $this->visibility = 'public';
6✔
57
            }
58

59
            $this->parentName = self::getFQN($parentNode->getAttribute('parent'));
6✔
60
        }
61

62
        $this->collectTags($node);
6✔
63

64
        if ($node->getAttribute('parent') instanceof ClassConst) {
6✔
65
            $this->parentName = static::getFQN($node->getAttribute('parent')->getAttribute('parent'));
6✔
66
        }
67

68
        return $this;
6✔
69
    }
70

71
    /**
72
     * @param ReflectionClassConstant $constant
73
     *
74
     * @return $this
75
     */
76
    public function readObjectFromReflection($constant): self
77
    {
78
        $this->name = $constant->getName();
7✔
79

80
        $file = $constant->getDeclaringClass()->getFileName();
7✔
81
        if ($file) {
7✔
82
            $this->file = $file;
7✔
83
        }
84

85
        /** @psalm-suppress InvalidPropertyAssignmentValue - upstream phpdoc error ? */
86
        $this->value = $constant->getValue();
7✔
87

88
        $this->type = \gettype($this->value);
7✔
89

90
        if ($constant->isPrivate()) {
7✔
91
            $this->visibility = 'private';
1✔
92
        } elseif ($constant->isProtected()) {
7✔
93
            $this->visibility = 'protected';
×
94
        } else {
95
            $this->visibility = 'public';
7✔
96
        }
97

98
        return $this;
7✔
99
    }
100

101
    protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
102
    {
103
        $parent = $node->getAttribute('parent');
7✔
104
        $parentParentNode = $parent ? $parent->getAttribute('parent') : null;
7✔
105

106
        if (
107
            $parentParentNode instanceof Namespace_
7✔
108
            &&
109
            $parentParentNode->name instanceof Name
7✔
110
        ) {
111
            $namespace = '\\' . \implode('\\', $parentParentNode->name->getParts()) . '\\';
3✔
112
        } else {
113
            $namespace = '';
7✔
114
        }
115

116
        return $namespace . $nodeName;
7✔
117
    }
118
}
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