GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0397c5...e9cf7d )
by Cees-Jan
08:11
created

UpdateNamespaces::operate()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 49
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 38
nc 12
nop 3
1
<?php declare(strict_types = 1);
2
3
namespace ApiClients\Tools\Installer\Operation;
4
5
use ApiClients\Tools\Installer\Filesystem;
6
use ApiClients\Tools\Installer\OperationInterface;
7
use BetterReflection\Reflector\ClassReflector;
8
use BetterReflection\SourceLocator\Type\StringSourceLocator;
9
use Composer\Factory;
10
use PhpParser\Node;
11
use PhpParser\ParserFactory;
12
use PhpParser\PrettyPrinter\Standard;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
final class UpdateNamespaces implements OperationInterface
16
{
17
    /**
18
     * @var Filesystem
19
     */
20
    private $filesystem;
21
22
    /**
23
     * @internal
24
     * @param Filesystem $filesystem
25
     */
26
    public function __construct(Filesystem $filesystem)
27
    {
28
        $this->filesystem = $filesystem;
29
    }
30
31
    public static function create(): OperationInterface
32
    {
33
        return new self(new Filesystem());
34
    }
35
36
    public function operate(array $replacements, array $environment, SymfonyStyle $style)
37
    {
38
        $classes = [];
39
        foreach ([
40
            'path_src' => ['ns_vendor', 'current_ns'],
41
            'path_tests' => ['ns_tests_vendor', 'current_ns_tests'],
42
        ] as $dirIndex => list($namespaceIndex, $currentNamespace)) {
43
            $oldNamespace = $environment[$currentNamespace] ?? '';
44
            $newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project'];
45
            $path = str_replace(
46
                'composer.json',
47
                $replacements[$dirIndex],
48
                Factory::getComposerFile()
49
            );
50
            $path = rtrim($path, '/');
51
            $path .= '/';
52
            foreach ($this->filesystem->ls($path) as $fileName) {
53
                $fileContents = $this->filesystem->read($fileName);
54
                $reflector = new ClassReflector(new StringSourceLocator($fileContents));
55
                foreach ($reflector->getAllClasses() as $class) {
56
                    $className = $class->getName();
57
                    $classes[$className] = str_replace($oldNamespace, $newNamespace, $className);
58
                }
59
            }
60
        }
61
62
        foreach ([
63
            'path_src' => ['ns_vendor', 'current_ns'],
64
            'path_tests' => ['ns_tests_vendor', 'current_ns_tests'],
65
        ] as $dirIndex => list($namespaceIndex, $currentNamespace)) {
66
            $oldNamespace = $environment[$currentNamespace] ?? '';
67
            $newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project'];
68
            $path = str_replace(
69
                'composer.json',
70
                $replacements[$dirIndex],
71
                Factory::getComposerFile()
72
            );
73
            $path = rtrim($path, '/');
74
            $path .= '/';
75
            foreach ($this->filesystem->ls($path) as $fileName) {
76
                $style->text(' * Updating ' . $fileName);
77
                $stmts = $this->parseFile($fileName);
78
                $stmts = $this->updateNamespaces($stmts, $newNamespace, $oldNamespace, $classes);
79
                $this->filesystem->write($fileName, (new Standard())->prettyPrintFile($stmts) . PHP_EOL);
0 ignored issues
show
Documentation introduced by
$stmts is of type null|array, but the function expects a array<integer,object<PhpParser\Node>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
            }
81
        }
82
83
        $style->success('Namespaces updated');
84
    }
85
86
    private function updateNamespaces(array $stmts, string $namespace, string $currentNamespace, $classes)
87
    {
88
        if ($stmts === null) {
89
            return;
90
        }
91
92
        foreach ($stmts as $index => $node) {
93
            if (!($node instanceof Node\Stmt\Namespace_)) {
94
                continue;
95
            }
96
97
            $suffix = str_replace($currentNamespace, '', (string)$node->name);
98
99
            $stmts[$index] = new Node\Stmt\Namespace_(
100
                new Node\Name(
101
                    $namespace . $suffix
102
                ),
103
                $this->updateUses($node->stmts, $classes),
0 ignored issues
show
Bug introduced by
It seems like $this->updateUses($node->stmts, $classes) targeting ApiClients\Tools\Install...amespaces::updateUses() can also be of type array; however, PhpParser\Node\Stmt\Namespace_::__construct() does only seem to accept null|array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
                $node->getAttributes()
105
            );
106
107
            break;
108
        }
109
110
        return $stmts;
111
    }
112
113
    private function updateUses(array $stmts, array $classes)
114
    {
115
        if ($stmts === null) {
116
            return;
117
        }
118
119
        foreach ($stmts as $index => $node) {
120
            if (!($node instanceof Node\Stmt\Use_)) {
121
                continue;
122
            }
123
124
            if ($node->type !== Node\Stmt\Use_::TYPE_NORMAL) {
125
                continue;
126
            }
127
128
            foreach ($node->uses as $useIndex => $useNode) {
129
                if (!($useNode instanceof Node\Stmt\UseUse)) {
130
                    continue;
131
                }
132
133
                if (!isset($classes[(string)$useNode->name])) {
134
                    continue;
135
                }
136
137
                $stmts[$index]->uses[$useIndex]->name = new Node\Name(
138
                    $classes[(string)$useNode->name]
139
                );
140
            }
141
        }
142
143
        return $stmts;
144
    }
145
146
    private function parseFile(string $fileName)
147
    {
148
        return (new ParserFactory())->create(ParserFactory::ONLY_PHP7)->parse($this->filesystem->read($fileName));
149
    }
150
}
151