Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.39% covered (danger)
48.39%
15 / 31
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
NamespacedSymbol
48.39% covered (danger)
48.39%
15 / 31
66.67% covered (warning)
66.67%
6 / 9
81.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOriginalFqdnName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getReplacementFqdnName
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNamespaceName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDoRename
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 isGlobal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isPsr0Autoloaded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPsr0NamespaceString
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This is used so NamespaceSymbol doesn't have a namespace property itself.
7 * Objects/classes inheriting from this could just be in the global namespace.
8 *
9 * @package brianhenryie/strauss
10 */
11
12namespace BrianHenryIE\Strauss\Types;
13
14use BrianHenryIE\Strauss\Composer\ComposerPackage;
15use BrianHenryIE\Strauss\Files\FileBase;
16use BrianHenryIE\Strauss\Files\FileWithDependency;
17
18class NamespacedSymbol extends DiscoveredSymbol
19{
20    protected NamespaceSymbol $namespace;
21
22    public function __construct(
23        string $fqdnSymbol,
24        FileBase $sourceFile,
25        NamespaceSymbol $namespace,
26        ?ComposerPackage $composerPackage = null
27    ) {
28        $this->namespace = $namespace;
29
30        parent::__construct($fqdnSymbol, $sourceFile, $composerPackage);
31    }
32
33    public function getOriginalFqdnName(): string
34    {
35        return $this->namespace->isGlobal()
36             ? $this->getOriginalLocalName()
37             : $this->namespace->getOriginalFqdnName() . '\\' . $this->getOriginalLocalName();
38    }
39
40    /**
41     * Defaults to the original until otherwise set.
42     */
43    public function getReplacementFqdnName(): string
44    {
45        if (!$this->isDoRename()) {
46            return $this->fqdnOriginalSymbol;
47        }
48        return $this->getNamespace()->isGlobal()
49            ? $this->getLocalReplacement()
50            : trim($this->namespace->getLocalReplacement() . '\\' . $this->getOriginalLocalName(), '\\');
51    }
52
53    public function getNamespace(): NamespaceSymbol
54    {
55        return $this->namespace;
56    }
57
58    public function getNamespaceName(): string
59    {
60        return $this->namespace->getOriginalFqdnName();
61    }
62
63    public function isDoRename(): bool
64    {
65        return parent::isDoRename()
66               && // If it has a non-global namespace, ensure that should be renamed.
67               ($this->namespace->isGlobal() || $this->namespace->isDoRename());
68    }
69
70    public function isGlobal(): bool
71    {
72        return $this->namespace->isGlobal();
73    }
74
75    public function isPsr0Autoloaded(): bool
76    {
77        return (bool) $this->getPsr0NamespaceString();
78    }
79
80    public function getPsr0NamespaceString(): ?string
81    {
82        /** @var ComposerPackage $dependency */
83        foreach ($this->dependencies as $dependency) {
84            if (! $dependency->isPsr0Autoloaded()) {
85                continue;
86            }
87            foreach ($this->getSourceFiles() as $file) {
88                if (! ( $file instanceof FileWithDependency )) {
89                    continue;
90                }
91                if ($file->getDependency()->getPackageName() === $dependency->getPackageName()) {
92                    /**
93                     * This is verified in {@see ComposerPackage::isPsr0Autoloaded()}.
94                     *
95                     * @var string $psr0namespace
96                     * @var string|string[] $autoloadPackageRelativePath
97                     * @phpstan-ignore offsetAccess.notFound
98                     */
99                    foreach ($dependency->getAutoload()['psr-0'] as $psr0namespace => $autoloadPackageRelativePath) {
100                        if (str_starts_with(
101                            trim($file->getPackageRelativePath(), '\\/'),
102                            trim($autoloadPackageRelativePath, '\\/')
103                        )) {
104                            return $psr0namespace;
105                        }
106                    }
107                }
108            }
109        }
110        return null;
111    }
112}