Completed
Pull Request — master (#1222)
by
unknown
01:34
created

ImagineStyle::line()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Component\Console\Style;
13
14
use Liip\ImagineBundle\Exception\InvalidArgumentException;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use Symfony\Component\Console\Terminal;
19
20
/**
21
 * @internal
22
 */
23
final class ImagineStyle implements ImagineStyleInterface
24
{
25
    /**
26
     * @var SymfonyStyle
27
     */
28
    private $io;
29
30
    /**
31
     * @var bool
32
     */
33
    private $decoration;
34
35
    public function __construct(InputInterface $input, OutputInterface $output, bool $decoration = true)
36
    {
37
        $this->io = new SymfonyStyle($input, $output);
38
        $this->decoration = $decoration;
39
    }
40
41
    public function text(string $string, array $replacements = []): ImagineStyleInterface
42
    {
43
        $this->io->write($this->compileString($string, $replacements));
44
45
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...ineStyleInterface::text of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
48
    public function line(string $string, array $replacements = []): ImagineStyleInterface
49
    {
50
        $this->io->writeln($this->compileString($string, $replacements));
51
52
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...ineStyleInterface::line of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
53
    }
54
55
    public function newline(int $count = 1): ImagineStyleInterface
56
    {
57
        $this->io->newLine($count);
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...StyleInterface::newline of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    public function space(int $count = 1): ImagineStyleInterface
63
    {
64
        return $this->text(str_repeat(' ', $count));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->text(str_repeat(' ', $count)); (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...neStyleInterface::space of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
    }
66
67
    public function separator(string $character = null, int $width = null, string $fg = null, bool $newline = true): ImagineStyleInterface
68
    {
69
        if (null === $width) {
70
            $width = (new Terminal())->getWidth();
71
        }
72
73
        $this->text('<fg=%2$s;>%1$s</>', [str_repeat($character ?: '-', $width), $fg ?: 'default']);
74
75
        if ($newline) {
76
            $this->newline();
77
        }
78
79
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...yleInterface::separator of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
80
    }
81
82
    public function status(string $status, string $fg = null, string $bg = null): ImagineStyleInterface
83
    {
84
        return $this->text(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->text(sprin...t', $bg ?: 'default')); (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...eStyleInterface::status of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
            sprintf('<fg=%2$s;bg=%3$s>(</><fg=%2$s;bg=%3$s;options=bold>%1$s</><fg=%2$s;bg=%3$s>)</>', $status, $fg ?: 'default', $bg ?: 'default')
86
        );
87
    }
88
89
    public function group(string $item, string $group, string $fg = null, string $bg = null): ImagineStyleInterface
90
    {
91
        return $this->text(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->text(sprin...t', $bg ?: 'default')); (Liip\ImagineBundle\Compo...sole\Style\ImagineStyle) is incompatible with the return type declared by the interface Liip\ImagineBundle\Compo...neStyleInterface::group of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
92
            sprintf('<fg=%3$s;bg=%4$s;options=bold>%1$s[</><fg=%3$s;bg=%4$s>%2$s</><fg=%3$s;bg=%4$s;options=bold>]</>', $item, $group, $fg ?: 'default', $bg ?: 'default')
93
        );
94
    }
95
96
    public function title(string $title, string $type = null, string $fg = null, string $bg = null): ImagineStyleInterface
97
    {
98
        if (!$this->decoration) {
99
            return $this->plainTitle($title, $type);
100
        }
101
102
        return $this->block($title, $type, $fg ?: 'white', $bg ?: 'magenta');
103
    }
104
105
    public function smallBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface
106
    {
107
        return $this->block($string, $type, $fg, $bg, $prefix, false);
108
    }
109
110
    public function largeBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface
111
    {
112
        return $this->block($string, $type, $fg, $bg, $prefix, true);
113
    }
114
115
    public function okayBlock(string $string, array $replacements = []): ImagineStyleInterface
116
    {
117
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'OKAY', 'black', 'green', '-');
118
    }
119
120
    public function noteBlock(string $string, array $replacements = []): ImagineStyleInterface
121
    {
122
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'NOTE', 'yellow', 'black', '/');
123
    }
124
125
    public function critBlock(string $string, array $replacements = []): ImagineStyleInterface
126
    {
127
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'ERROR', 'white', 'red', '#');
128
    }
129
130
    private function plainTitle(string $title, string $type = null): ImagineStyleInterface
131
    {
132
        $this->newline();
133
134
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
135
            $this->line('# [%s] %s', [$type, $title]);
136
        } else {
137
            $this->line('# %s', [$title]);
138
        }
139
140
        return $this->newline();
141
    }
142
143
    private function block(string $string, string $type = null, string $fg = null, string $bg = null, string $prefix = null, bool $padding = true): ImagineStyleInterface
144
    {
145
        if (!$this->decoration) {
146
            return $this->plainBlock($string, $type);
147
        }
148
149
        $this->io->block($string, $type, sprintf('fg=%s;bg=%s', $fg ?: 'default', $bg ?: 'default'), $prefix ? sprintf(' %s ', $prefix) : ' ', $padding);
150
151
        return $this;
152
    }
153
154
    private function plainBlock(string $string, string $type): ImagineStyleInterface
155
    {
156
        return $this
157
            ->newline()
158
            ->line('[%s] %s', [$type, $string])
159
            ->newline();
160
    }
161
162
    private function compileString(string $format, array $replacements = []): string
163
    {
164
        if (!$this->decoration) {
165
            $format = strip_tags($format);
166
        }
167
168
        if (0 === count($replacements)) {
169
            return $format;
170
        }
171
172
        if (false !== $compiled = @vsprintf($format, $replacements)) {
173
            return $compiled;
174
        }
175
176
        throw new InvalidArgumentException(
177
            sprintf('Invalid string format "%s" or replacements "%s".', $format, implode(', ', array_map(function ($replacement) {
178
                return var_export($replacement, true);
179
            }, $replacements)))
180
        );
181
    }
182
}
183