ScoperLogger::outputFileCount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 7
rs 10
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 Fidry\Console\Application\Application as FidryApplication;
18
use Fidry\Console\Input\IO;
19
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
20
use Symfony\Component\Console\Helper\ProgressBar;
21
use Symfony\Component\Console\Output\NullOutput;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use function count;
24
use function memory_get_peak_usage;
25
use function memory_get_usage;
26
use function microtime;
27
use function round;
28
use function Safe\sprintf;
29
30
/**
31
 * @private
32
 * @final
33
 * @codeCoverageIgnore
34
 */
35
class ScoperLogger
36
{
37
    private FidryApplication $application;
38
    private IO $io;
39
    private float $startTime;
40
    private ProgressBar $progressBar;
41
42
    public function __construct(FidryApplication $application, IO $io)
43
    {
44
        $this->io = $io;
45
        $this->application = $application;
46
        $this->startTime = microtime(true);
47
        $this->progressBar = new ProgressBar(new NullOutput());
48
    }
49
50
    /**
51
     * @param string[] $paths
52
     */
53
    public function outputScopingStart(?string $prefix, array $paths): void
54
    {
55
        $this->io->writeln($this->application->getHelp());
56
57
        $newLine = 1;
58
59
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
60
            $this->io->section('Input');
61
62
            $this->io->writeln(
63
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
                    'Prefix: %s',
65
                    $prefix,
66
                ),
67
            );
68
69
            $this->io->write('Paths:');
70
71
            if (0 === count($paths)) {
72
                $this->io->writeln(' Loaded from config');
73
            } else {
74
                $this->io->writeln('');
75
                $this->io->listing($paths);
76
            }
77
78
            $this->io->section('Processing');
79
            $newLine = 0;
80
        }
81
82
        $this->io->newLine($newLine);
83
    }
84
85
    /**
86
     * Output file count message if relevant.
87
     */
88
    public function outputFileCount(int $count): void
89
    {
90
        if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) {
91
            $this->progressBar = $this->io->createProgressBar($count);
92
            $this->progressBar->start();
93
        } elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
94
            $this->progressBar = new ProgressBar(new NullOutput());
95
        }
96
    }
97
98
    /**
99
     * Output scoping success message.
100
     */
101
    public function outputSuccess(string $path): void
102
    {
103
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
104
            $this->io->writeln(
105
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106
                    ' * [<info>OK</info>] %s',
107
                    $path,
108
                ),
109
            );
110
        }
111
112
        $this->progressBar->advance();
113
    }
114
115
    public function outputWarnOfFailure(string $path, ParsingException $exception): void
116
    {
117
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
118
            $this->io->writeln(
119
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
                    ' * [<error>NO</error>] %s',
121
                    $path,
122
                ),
123
            );
124
        }
125
126
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
127
            $this->io->writeln(
128
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
129
                    "\t".'%s: %s',
130
                    $exception->getMessage(),
131
                    (string) $exception->getPrevious(),
132
                ),
133
            );
134
        }
135
136
        $this->progressBar->advance();
137
    }
138
139
    public function outputScopingEnd(): void
140
    {
141
        $this->finish(false);
142
    }
143
144
    public function outputScopingEndWithFailure(): void
145
    {
146
        $this->finish(true);
147
    }
148
149
    private function finish(bool $failed): void
150
    {
151
        $this->progressBar->finish();
152
        $this->io->newLine(2);
153
154
        if (!$failed) {
155
            $this->io->success(
156
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

156
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
157
                    'Successfully prefixed %d files.',
158
                    $this->progressBar->getMaxSteps(),
159
                ),
160
            );
161
        }
162
163
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
164
            $this->io->comment(
165
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

165
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
166
                    '<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>',
167
                    round(memory_get_usage() / 1024 / 1024, 2),
168
                    round(memory_get_peak_usage() / 1024 / 1024, 2),
169
                    round(microtime(true) - $this->startTime, 2),
170
                ),
171
            );
172
        }
173
    }
174
}
175