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

tempestphp / tempest-framework / 11331827224

14 Oct 2024 04:52PM UTC coverage: 80.589% (-1.4%) from 82.008%
11331827224

Pull #568

github

web-flow
Merge 57ab8f089 into 158b2db46
Pull Request #568: feat(generator/console): add make:controller command

53 of 206 new or added lines in 8 files covered. (25.73%)

10 existing lines in 5 files now uncovered.

6813 of 8454 relevant lines covered (80.59%)

39.52 hits per line

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

90.77
/src/Tempest/Generation/src/SimplifiesClassNames.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Generation;
6

7
use Nette\PhpGenerator\PhpFile;
8
use function Tempest\Support\str;
9

10
/**
11
 * @internal
12
 */
13
trait SimplifiesClassNames
14
{
15
    private bool $simplifyImplements = true;
16

17
    private bool $simplifyClassNamesInBodies = true;
18

19
    private array $aliases = [];
20

21
    public function simplifyClassNamesInMethodBodies(bool $simplify = true): self
2✔
22
    {
23
        $this->simplifyClassNamesInBodies = $simplify;
2✔
24

25
        return $this;
2✔
26
    }
27

28
    public function simplifyImplements(bool $simplify = true): self
3✔
29
    {
30
        $this->simplifyImplements = $simplify;
3✔
31

32
        return $this;
3✔
33
    }
34

35
    public function addUse(string $use): self
×
36
    {
37
        $this->aliases[$use] = null;
×
38

39
        return $this;
×
40
    }
41

42
    public function setAlias(string $class, string $alias): self
1✔
43
    {
44
        if (isset($this->aliases[$class])) {
1✔
45
            unset($this->aliases[$class]);
×
46
        }
47

48
        $this->aliases[$class] = $alias;
1✔
49

50
        return $this;
1✔
51
    }
52

53
    private function simplifyClassNames(PhpFile $file): PhpFile
22✔
54
    {
55
        foreach ($file->getNamespaces() as $namespace) {
22✔
56
            foreach ($namespace->getClasses() as $class) {
22✔
57
                $types = [];
22✔
58
                $functions = [];
22✔
59

60
                if ($this->simplifyImplements) {
22✔
61
                    foreach ($class->getImplements() as $implement) {
20✔
62
                        $types[] = $implement;
13✔
63
                    }
64
                }
65

66
                foreach ($class->getAttributes() as $attribute) {
22✔
67
                    $types[] = $attribute->getName();
12✔
68
                }
69

70
                foreach ($class->getMethods() as $method) {
22✔
71
                    $types[] = $method->getReturnType(true);
18✔
72

73
                    foreach ($method->getParameters() as $parameter) {
18✔
UNCOV
74
                        $types[] = $parameter->getType(true);
×
75
                    }
76

77
                    foreach ($method->getAttributes() as $attribute) {
18✔
78
                        $types[] = $attribute->getName();
2✔
79
                    }
80

81
                    if ($this->simplifyClassNamesInBodies) {
18✔
82
                        $methodBody = $method->getBody();
16✔
83
                        $fqcnMatches = $this->extractFqcnFromBody($methodBody);
16✔
84

85
                        foreach (array_filter($fqcnMatches) as $fqcn) {
16✔
86
                            if (str_contains($methodBody, "/*(f*/\\{$fqcn}")) {
16✔
87
                                $namespace->addUseFunction($fqcn);
2✔
88

89
                                continue;
2✔
90
                            }
91

92
                            if (str_contains($methodBody, "/*(n*/\\{$fqcn}")) {
14✔
93
                                $namespace->addUse($fqcn);
12✔
94

95
                                continue;
12✔
96
                            }
97

98
                            $methodBody = str_replace(
2✔
99
                                search: '\\'.$fqcn,
2✔
100
                                replace: (string) str($fqcn)->afterLast('\\'),
2✔
101
                                subject: $methodBody
2✔
102
                            );
2✔
103

104
                            $namespace->addUse($fqcn);
2✔
105
                        }
106

107
                        $method->setBody($methodBody);
16✔
108
                    }
109
                }
110

111
                array_map(function ($param) use (&$types): void {
22✔
112
                    $types[] = $param->getType(true);
×
113
                }, $class->getProperties());
22✔
114

115
                foreach ($this->aliases as $class => $alias) {
22✔
116
                    $namespace->addUse($class, $alias);
1✔
117
                }
118

119
                foreach (array_filter($types) as $type) {
22✔
120
                    if (is_string($type)) {
19✔
121
                        $namespace->addUse($type);
16✔
122

123
                        continue;
16✔
124
                    }
125

126
                    foreach ($type->getTypes() as $subtype) {
18✔
127
                        if ($subtype->isClass() && ! $subtype->isClassKeyword()) {
18✔
128
                            $namespace->addUse((string) $subtype);
18✔
129
                        }
130
                    }
131
                }
132
            }
133
        }
134

135
        return $file;
22✔
136
    }
137

138
    private function extractFqcnFromBody(string $body): array
16✔
139
    {
140
        preg_match_all('/(?:\\\\?[A-Za-z_][\w\d_]*\\\\)+[A-Za-z_][\w\d_]*/', $body, $matches);
16✔
141

142
        return array_filter(array_unique(
16✔
143
            array_map(fn (string $fqcn) => rtrim(ltrim($fqcn, '\\'), ':'), $matches[0])
16✔
144
        ));
16✔
145
    }
146
}
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