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.

UpdateNamespaces::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 1
    public function __construct(Filesystem $filesystem)
27
    {
28 1
        $this->filesystem = $filesystem;
29 1
    }
30
31
    public static function create(): OperationInterface
32
    {
33
        return new self(new Filesystem());
34
    }
35
36 1
    public function operate(array $replacements, array $configuration, SymfonyStyle $style)
37
    {
38 1
        $classes = [];
39
        foreach ([
40 1
            'path_src' => ['ns_vendor', 'current_ns'],
41
            'path_tests' => ['ns_tests_vendor', 'current_ns_tests'],
42
        ] as $dirIndex => list($namespaceIndex, $currentNamespace)) {
43 1
            $oldNamespace = $configuration[$currentNamespace] ?? '';
44 1
            $newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project'];
45 1
            $path = str_replace(
46 1
                'composer.json',
47 1
                $replacements[$dirIndex],
48 1
                Factory::getComposerFile()
49
            );
50 1
            $path = rtrim($path, '/');
51 1
            $path .= '/';
52 1
            foreach ($this->filesystem->ls($path) as $fileName) {
53 1
                $fileContents = $this->filesystem->read($fileName);
54 1
                $reflector = new ClassReflector(new StringSourceLocator($fileContents));
55 1
                foreach ($reflector->getAllClasses() as $class) {
56 1
                    $className = $class->getName();
57 1
                    $classes[$className] = str_replace($oldNamespace, $newNamespace, $className);
58
                }
59
            }
60
        }
61
62
        foreach ([
63 1
            'path_src' => ['ns_vendor', 'current_ns'],
64
            'path_tests' => ['ns_tests_vendor', 'current_ns_tests'],
65
        ] as $dirIndex => list($namespaceIndex, $currentNamespace)) {
66 1
            $oldNamespace = $configuration[$currentNamespace] ?? '';
67 1
            $newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project'];
68 1
            $path = str_replace(
69 1
                'composer.json',
70 1
                $replacements[$dirIndex],
71 1
                Factory::getComposerFile()
72
            );
73 1
            $path = rtrim($path, '/');
74 1
            $path .= '/';
75 1
            foreach ($this->filesystem->ls($path) as $fileName) {
76 1
                $style->text(' * Updating ' . $fileName);
77 1
                $stmts = $this->parseFile($fileName);
78 1
                $stmts = $this->updateNamespaces($stmts, $newNamespace, $oldNamespace, $classes);
79 1
                $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 1
        $style->success('Namespaces updated');
84 1
    }
85
86 1
    private function updateNamespaces(array $stmts, string $namespace, string $currentNamespace, $classes)
87
    {
88 1
        if ($stmts === null) {
89
            return;
90
        }
91
92 1
        foreach ($stmts as $index => $node) {
93 1
            if (!($node instanceof Node\Stmt\Namespace_)) {
94
                continue;
95
            }
96
97 1
            $suffix = str_replace($currentNamespace, '', (string)$node->name);
98
99 1
            $stmts[$index] = new Node\Stmt\Namespace_(
100 1
                new Node\Name(
101 1
                    $namespace . $suffix
102
                ),
103 1
                $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 1
                $node->getAttributes()
105
            );
106
107 1
            break;
108
        }
109
110 1
        return $stmts;
111
    }
112
113 1
    private function updateUses(array $stmts, array $classes)
114
    {
115 1
        if ($stmts === null) {
116
            return;
117
        }
118
119 1
        foreach ($stmts as $index => $node) {
120 1
            if (!($node instanceof Node\Stmt\Use_)) {
121 1
                continue;
122
            }
123
124 1
            if ($node->type !== Node\Stmt\Use_::TYPE_NORMAL) {
125
                continue;
126
            }
127
128 1
            foreach ($node->uses as $useIndex => $useNode) {
129 1
                if (!($useNode instanceof Node\Stmt\UseUse)) {
130
                    continue;
131
                }
132
133 1
                if (!isset($classes[(string)$useNode->name])) {
134
                    continue;
135
                }
136
137 1
                $stmts[$index]->uses[$useIndex]->name = new Node\Name(
138 1
                    $classes[(string)$useNode->name]
139
                );
140
            }
141
        }
142
143 1
        return $stmts;
144
    }
145
146 1
    private function parseFile(string $fileName)
147
    {
148 1
        return (new ParserFactory())->create(ParserFactory::ONLY_PHP7)->parse($this->filesystem->read($fileName));
149
    }
150
}
151