PrepareTerminalWidthOption   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 99
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setApplication() 0 4 1
A setTerminal() 0 4 1
A getTerminal() 0 7 3
A enableWrap() 0 4 1
A prepare() 0 13 2
A getTerminalWidth() 0 14 3
A getTerminalWidthViaApplication() 0 12 3
A getMaxWidth() 0 4 1
A getMinWidth() 0 4 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Options;
3
4
use Symfony\Component\Console\Application;
5
use Consolidation\AnnotatedCommand\CommandData;
6
use Consolidation\OutputFormatters\Options\FormatterOptions;
7
8
class PrepareTerminalWidthOption implements PrepareFormatter
9
{
10
    /** var Application */
11
    protected $application;
12
13
    protected $terminal;
14
15
    /** var int */
16
    protected $defaultWidth;
17
18
    /** var int */
19
    protected $maxWidth = PHP_INT_MAX;
20
21
    /** var int */
22
    protected $minWidth = 0;
23
24
    /* var boolean */
25
    protected $shouldWrap = true;
26
27
    public function __construct($defaultWidth = 0)
28
    {
29
        $this->defaultWidth = $defaultWidth;
30
    }
31
32
    public function setApplication(Application $application)
33
    {
34
        $this->application = $application;
35
    }
36
37
    public function setTerminal($terminal)
38
    {
39
        $this->terminal = $terminal;
40
    }
41
42
    public function getTerminal()
43
    {
44
        if (!$this->terminal && class_exists('\Symfony\Component\Console\Terminal')) {
45
            $this->terminal = new \Symfony\Component\Console\Terminal();
46
        }
47
        return $this->terminal;
48
    }
49
50
    public function enableWrap($shouldWrap)
51
    {
52
        $this->shouldWrap = $shouldWrap;
53
    }
54
55
    public function prepare(CommandData $commandData, FormatterOptions $options)
56
    {
57
        $width = $this->getTerminalWidth();
58
        if (!$width) {
59
            $width = $this->defaultWidth;
60
        }
61
62
        // Enforce minimum and maximum widths
63
        $width = min($width, $this->getMaxWidth($commandData));
64
        $width = max($width, $this->getMinWidth($commandData));
65
66
        $options->setWidth($width);
67
    }
68
69
    protected function getTerminalWidth()
70
    {
71
        // Don't wrap if wrapping has been disabled.
72
        if (!$this->shouldWrap) {
73
            return 0;
74
        }
75
76
        $terminal = $this->getTerminal();
77
        if ($terminal) {
78
            return $terminal->getWidth();
79
        }
80
81
        return $this->getTerminalWidthViaApplication();
82
    }
83
84
    protected function getTerminalWidthViaApplication()
85
    {
86
        if (!$this->application) {
87
            return 0;
88
        }
89
        $dimensions = $this->application->getTerminalDimensions();
0 ignored issues
show
Bug introduced by
The method getTerminalDimensions() does not seem to exist on object<Symfony\Component\Console\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        if ($dimensions[0] == null) {
91
            return 0;
92
        }
93
94
        return $dimensions[0];
95
    }
96
97
    protected function getMaxWidth(CommandData $commandData)
0 ignored issues
show
Unused Code introduced by
The parameter $commandData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        return $this->maxWidth;
100
    }
101
102
    protected function getMinWidth(CommandData $commandData)
0 ignored issues
show
Unused Code introduced by
The parameter $commandData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        return $this->minWidth;
105
    }
106
}
107