Test Failed
Pull Request — master (#28)
by Pádraic
02:32
created

AddPrefixCommandHandler::handle()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 9
nop 3
1
<?php
2
3
/*
4
 * This file is part of the webmozart/php-scoper package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\PhpScoper\Handler;
13
14
use PhpParser\ParserFactory;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Finder\Finder;
17
use Webmozart\PhpScoper\Exception\ParsingException;
18
use Webmozart\PhpScoper\Exception\RuntimeException;
19
use Webmozart\PhpScoper\Formatter\BasicFormatter;
20
use Webmozart\PhpScoper\Scoper;
21
22
/**
23
 * Handles the "add-prefix" command.
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class AddPrefixCommandHandler
28
{
29
    /**
30
     * @var Filesystem
31
     */
32
    private $filesystem;
33
34
    /**
35
     * @var Finder
36
     */
37
    private $finder;
38
39
    /**
40
     * @var Scoper
41
     */
42
    private $scoper;
43
44
    public function __construct(array $declaredClasses = [])
45
    {
46
        $this->filesystem = new Filesystem();
47
        $this->finder = new Finder();
48
49
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
50
        $this->scoper = new Scoper($parser, $declaredClasses);
51
    }
52
53
    /**
54
     * Handles the "add-prefix" command.
55
     *
56
     * @param string         $prefix
57
     * @param string[]       $paths
58
     * @param BasicFormatter $formatter
59
     *
60
     * @return int Returns 0 on success and a positive integer on error.
61
     */
62
    public function handle($prefix, array $paths, BasicFormatter $formatter)
63
    {
64
        $prefix = rtrim($prefix, '\\');
65
        $pathsToSearch = [];
66
        $filesToAppend = [];
67
68
        foreach ($paths as $path) {
69
            if (!$this->filesystem->isAbsolutePath($path)) {
70
                $path = getcwd().DIRECTORY_SEPARATOR.$path;
71
            }
72
73
            if (($exists = !file_exists($path)) || !is_readable($path)) {
74
                $issue = $exists ? 'does not exist' : 'is not readable';
75
                throw new RuntimeException(sprintf(
76
                    'A given path %s: %s',
77
                    $issue,
78
                    $path
79
                ));
80
            }
81
82
            if (is_dir($path)) {
83
                $pathsToSearch[] = $path;
84
            } else {
85
                $filesToAppend[] = $path;
86
            }
87
        }
88
89
        $this->finder->files()
90
            ->name('*.php')
91
            ->in($pathsToSearch)
92
            ->append($filesToAppend)
93
            ->sortByName();
94
95
        $this->scopeFiles($prefix, $formatter);
96
97
        return 0;
98
    }
99
100
    /**
101
     * Scopes all files attached to Finder instance.
102
     *
103
     * @param string         $prefix
104
     * @param BasicFormatter $formatter
105
     */
106
    private function scopeFiles($prefix, BasicFormatter $formatter)
107
    {
108
        $count = count($this->finder);
109
        $formatter->outputFileCount($count);
110
111
        foreach ($this->finder as $file) {
112
            $this->scopeFile($file->getPathName(), $prefix, $formatter);
113
        }
114
    }
115
116
    /**
117
     * Scopes a given file.
118
     *
119
     * @param string         $path
120
     * @param string         $prefix
121
     * @param BasicFormatter $formatter
122
     */
123 View Code Duplication
    private function scopeFile($path, $prefix, BasicFormatter $formatter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $fileContent = file_get_contents($path);
126
        try {
127
            $scoppedContent = $this->scoper->addNamespacePrefix($fileContent, $prefix);
128
            $this->filesystem->dumpFile($path, $scoppedContent);
129
            $formatter->outputSuccess($path);
130
        } catch (ParsingException $exception) {
0 ignored issues
show
Bug introduced by
The class Webmozart\PhpScoper\Exception\ParsingException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
131
            $formatter->outputFail($path);
132
        }
133
    }
134
}
135