Passed
Branch master (c976c6)
by Théo
03:51
created

ScoperLogger::outputScopingStart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 2
dl 0
loc 30
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Console;
16
17
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
18
use Symfony\Component\Console\Application;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Humbug\PhpScoper\Console\Application. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Symfony\Component\Console\Helper\ProgressBar;
20
use Symfony\Component\Console\Output\NullOutput;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * @private
26
 * @final
27
 */
28
class ScoperLogger
29
{
30
    private $application;
31
    private $io;
32
    private $startTime;
33
    private $progressBar;
34
35
    public function __construct(Application $application, SymfonyStyle $io)
36
    {
37
        $this->io = $io;
38
        $this->application = $application;
39
        $this->startTime = microtime(true);
40
        $this->progressBar = new ProgressBar(new NullOutput());
41
    }
42
43
    /**
44
     * @param string   $prefix
45
     * @param string[] $paths
46
     */
47
    public function outputScopingStart(string $prefix, array $paths): void
48
    {
49
        $this->io->writeln($this->application->getHelp());
50
51
        $newLine = 1;
52
53
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
54
            $this->io->section('Input');
55
56
            $this->io->writeln(
57
                sprintf(
58
                    'Prefix: %s',
59
                    $prefix
60
                )
61
            );
62
63
            $this->io->write('Paths:');
64
65
            if (0 === count($paths)) {
66
                $this->io->writeln(' Loaded from config');
67
            } else {
68
                $this->io->writeln('');
69
                $this->io->listing($paths);
70
            }
71
72
            $this->io->section('Processing');
73
            $newLine = 0;
74
        }
75
76
        $this->io->newLine($newLine);
77
    }
78
79
    /**
80
     * Output file count message if relevant.
81
     *
82
     * @param int $count
83
     */
84
    public function outputFileCount(int $count): void
85
    {
86
        if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) {
87
            $this->progressBar = $this->io->createProgressBar($count);
88
            $this->progressBar->start();
89
        } elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
90
            $this->progressBar = new ProgressBar(new NullOutput());
91
        }
92
    }
93
94
    /**
95
     * Output scoping success message.
96
     *
97
     * @param string $path
98
     */
99
    public function outputSuccess(string $path): void
100
    {
101
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
102
            $this->io->writeln(
103
                sprintf(
104
                    ' * [<info>OK</info>] %s',
105
                    $path
106
                )
107
            );
108
        }
109
110
        $this->progressBar->advance();
111
    }
112
113
    public function outputWarnOfFailure(string $path, ParsingException $exception): void
114
    {
115
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
116
            $this->io->writeln(
117
                sprintf(
118
                    ' * [<error>NO</error>] %s',
119
                    $path
120
                )
121
            );
122
        }
123
124
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
125
            $this->io->writeln(
126
                sprintf(
127
                    "\t".'%s: %s',
128
                    $exception->getMessage(),
129
                    $exception->getPrevious()
130
                )
131
            );
132
        }
133
134
        $this->progressBar->advance();
135
    }
136
137
    /**
138
     * Output scoping failure message.
139
     *
140
     * @param string $path
141
     */
142
    public function outputFail(string $path): void
143
    {
144
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
145
            $this->io->writeln(
146
                sprintf(
147
                    ' * [<error>FA</error>] %s',
148
                    $path
149
                )
150
            );
151
        }
152
153
        $this->progressBar->advance();
154
    }
155
156
    public function outputScopingEnd(): void
157
    {
158
        $this->finish(false);
159
    }
160
161
    public function outputScopingEndWithFailure(): void
162
    {
163
        $this->finish(true);
164
    }
165
166
    private function finish(bool $failed): void
167
    {
168
        $this->progressBar->finish();
169
        $this->io->newLine(2);
170
171
        if (false === $failed) {
172
            $this->io->success(
173
                sprintf(
174
                    'Successfully prefixed %d files.',
175
                    $this->progressBar->getMaxSteps()
176
                )
177
            );
178
        }
179
180
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
181
            $this->io->comment(
182
                sprintf(
183
                    '<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>',
184
                    round(memory_get_usage() / 1024 / 1024, 2),
185
                    round(memory_get_peak_usage() / 1024 / 1024, 2),
186
                    round(microtime(true) - $this->startTime, 2)
187
                )
188
            );
189
        }
190
    }
191
}
192