AnsiFormatter::removeFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[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
namespace Webmozart\Console\Formatter;
13
14
use Symfony\Component\Console\Formatter\OutputFormatter;
15
use Webmozart\Console\Adapter\StyleConverter;
16
use Webmozart\Console\Api\Formatter\Formatter;
17
use Webmozart\Console\Api\Formatter\Style;
18
use Webmozart\Console\Api\Formatter\StyleSet;
19
20
/**
21
 * A formatter that replaces style tags by ANSI format codes.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class AnsiFormatter implements Formatter
28
{
29
    /**
30
     * @var OutputFormatter
31
     */
32
    private $innerFormatter;
33
34
    /**
35
     * Creates the formatter.
36
     *
37
     * @param StyleSet $styleSet The style set to use.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $styleSet not be null|StyleSet?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     */
39 146 View Code Duplication
    public function __construct(StyleSet $styleSet = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41 146
        $this->innerFormatter = new OutputFormatter(true);
42
43 146
        if (!$styleSet) {
44 143
            $styleSet = new DefaultStyleSet();
45
        }
46
47 146
        foreach ($styleSet->toArray() as $tag => $style) {
48 146
            $this->innerFormatter->setStyle($tag, StyleConverter::convert($style));
49
        }
50 146
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    public function format($string, Style $style = null)
56
    {
57 2
        if (null !== $style) {
58 1
            $this->innerFormatter->getStyleStack()->push(StyleConverter::convert($style));
59
        }
60
61 2
        $formatted = $this->innerFormatter->format($string);
62
63 2
        if (null !== $style) {
64 1
            $this->innerFormatter->getStyleStack()->pop();
65
        }
66
67 2
        return $formatted;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function removeFormat($string)
74
    {
75 1
        $this->innerFormatter->setDecorated(false);
76 1
        $formatted = $this->innerFormatter->format($string);
77 1
        $this->innerFormatter->setDecorated(true);
78
79 1
        return $formatted;
80
    }
81
}
82