Completed
Pull Request — master (#102)
by Greg
06:23
created

PrepareTerminalWidthOption::getMinWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
    /** var int */
14
    protected $defaultWidth;
15
16
    /** var int */
17
    protected $maxWidth = PHP_INT_MAX;
18
19
    /** var int */
20
    protected $minWidth = 0;
21
22
    /* var boolean */
23
    protected $shouldWrap = true;
24
25
    public function __construct($defaultWidth = 0)
26
    {
27
        $this->defaultWidth = $defaultWidth;
28
29
        // If STDOUT is not attached to a terminal, then disable
30
        // automatic width detection.
31
        if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) {
32
            $this->shouldWrap = false;
33
        }
34
    }
35
36
    public function setApplication(Application $application)
37
    {
38
        $this->application = $application;
39
    }
40
41
    public function enableWrap($shouldWrap)
42
    {
43
        $this->shouldWrap = $shouldWrap;
44
    }
45
46
    public function prepare(CommandData $commandData, FormatterOptions $options)
47
    {
48
        $width = $this->getTerminalWidth();
49
        if (!$width) {
50
            $width = $this->defaultWidth;
51
        }
52
53
        // Enforce minimum and maximum widths
54
        $width = min($width, $this->getMaxWidth($commandData));
55
        $width = max($width, $this->getMinWidth($commandData));
56
57
        $options->setWidth($width);
58
    }
59
60
    protected function getTerminalWidth()
61
    {
62
        if (!$this->application || !$this->shouldWrap) {
63
            return 0;
64
        }
65
66
        $dimensions = $this->application->getTerminalDimensions();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Consol...getTerminalDimensions() has been deprecated with message: since version 3.2, to be removed in 4.0. Create a Terminal instance instead.

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

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

Loading history...
67
        if ($dimensions[0] == null) {
68
            return 0;
69
        }
70
71
        return $dimensions[0];
72
    }
73
74
    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...
75
    {
76
        return $this->maxWidth;
77
    }
78
79
    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...
80
    {
81
        return $this->minWidth;
82
    }
83
}
84