Passed
Push — renovate/php-8.x ( bfb893...222c0a )
by
unknown
02:20
created

TopLikeOutputter::output()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 65
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 53
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 65
rs 9.0254

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the sj-i/ package.
5
 *
6
 * (c) sji <[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
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Output\TopLike;
15
16
use Symfony\Component\Console\Helper\Table;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Helper\TableCell;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\TableCell was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Console\Helper\TableCellStyle;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\TableCellStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Symfony\Component\Console\Output\ConsoleOutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...\ConsoleOutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Symfony\Component\Console\Terminal;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Terminal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
final class TopLikeOutputter implements Outputter
23
{
24
    public function __construct(
25
        private ConsoleOutputInterface $output,
26
        private Terminal $terminal,
27
    ) {
28
    }
29
30
    public function display(string $trace_target, Stat $stat): void
31
    {
32
        $stat->updateStat();
33
        $this->output($trace_target, $stat);
34
        $stat->clearCurrentSamples();
35
    }
36
37
    private function output(string $trace_target, Stat $stat): void
38
    {
39
        $this->output->write("\e[H\e[2J");
40
        $this->output->writeln($trace_target);
41
        $this->output->writeln(
42
            sprintf(
43
                'samp_count=%d  func_count=%d  total_count=%d',
44
                $stat->sample_count,
45
                count($stat->function_entries),
46
                $stat->total_count
47
            )
48
        );
49
        $this->output->writeln('');
50
        $count = 7;
51
        $width = $this->terminal->getWidth();
52
        $height = $this->terminal->getHeight();
53
        $padding_name = max(0, $width - 41);
54
55
        $rows = [];
56
        $align_right = new TableCellStyle(['align' => 'right']);
57
        $styled = fn (int|string $content, TableCellStyle $style): TableCell =>
58
            new TableCell(
59
                (string)$content,
60
                ['style' => $style]
61
            )
62
        ;
63
        foreach ($stat->function_entries as $function_entry) {
64
            $name = $function_entry->name;
65
            $percent = number_format($function_entry->percent_exclusive, 2);
66
            $rows[] = [
67
                $styled($function_entry->total_count_inclusive, $align_right),
68
                $styled($function_entry->total_count_exclusive, $align_right),
69
                $styled($function_entry->count_inclusive, $align_right),
70
                $styled($function_entry->count_exclusive, $align_right),
71
                $styled($percent, $align_right),
72
                $name,
73
            ];
74
            if (++$count > $height) {
75
                break;
76
            }
77
        }
78
79
        $output = $this->output->section();
80
        $table = new Table($output);
81
        $table->setColumnMaxWidth(5, max(4, $width - 41));
82
        $table->setHeaders([
83
            $styled('total_incl', $align_right),
84
            $styled('total_excl', $align_right),
85
            $styled('incl', $align_right),
86
            $styled('excl', $align_right),
87
            $styled('%', $align_right),
88
            str_pad('name', $padding_name),
89
        ]);
90
        $table->setRows($rows);
91
        $table->setStyle('compact');
92
        $table->getStyle()->setCellHeaderFormat('%s');
93
        $table->render();
94
        $output->overwrite(
95
            preg_replace(
96
                '/( *total_incl.*)/',
97
                '<bg=bright-white;fg=black>$1</>',
98
                preg_replace(
99
                    '/\e[[][A-Za-z0-9]*;?[0-9]*m?/',
100
                    '',
101
                    $output->getContent()
102
                )
103
            )
104
        );
105
    }
106
}
107