LazyWrite   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 21.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 16
loc 75
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 8 11 4
A writeln() 8 11 3
A writeAll() 0 6 2
A isLazyWrite() 0 4 1
A setLazyWrite() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Console\Output;
11
12
/**
13
 * Lazy write output.
14
 *
15
 * @author  Peter Gribanov <[email protected]>
16
 */
17
class LazyWrite extends Decorator
18
{
19
    /**
20
     * Write stack.
21
     *
22
     * @var array
23
     */
24
    protected $stack = [];
25
26
    /**
27
     * Save messages in stack.
28
     *
29
     * @var bool
30
     */
31
    protected $lazy_write = true;
32
33
    /**
34
     * @param array|string $messages
35
     * @param bool|false $newline
36
     * @param int $type
37
     */
38 30
    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
39
    {
40 30 View Code Duplication
        if ($this->lazy_write) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41 24
            $messages = (array) $messages;
42 24
            foreach ($messages as $message) {
43 24
                $this->stack[] = [$message.($newline ? PHP_EOL : ''), $type];
44 24
            }
45 24
        } else {
46 6
            parent::write($messages, $newline, $type);
47
        }
48 30
    }
49
50
    /**
51
     * @param array|string $messages
52
     * @param int $type
53
     */
54 15
    public function writeln($messages, $type = self::OUTPUT_NORMAL)
55
    {
56 15 View Code Duplication
        if ($this->lazy_write) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57 12
            $messages = (array) $messages;
58 12
            foreach ($messages as $message) {
59 12
                $this->stack[] = [$message, $type];
60 12
            }
61 12
        } else {
62 3
            parent::writeln($messages, $type);
63
        }
64 15
    }
65
66
    /**
67
     * Write all messages from stack.
68
     */
69 18
    public function writeAll()
70
    {
71 18
        while ($message = array_shift($this->stack)) {
72 18
            parent::writeln($message[0], $message[1]);
73 18
        }
74 18
    }
75
76
    /**
77
     * @return bool
78
     */
79 1
    public function isLazyWrite()
80
    {
81 1
        return $this->lazy_write;
82
    }
83
84
    /**
85
     * @param bool $lazy_write
86
     */
87 10
    public function setLazyWrite($lazy_write)
88
    {
89 10
        $this->lazy_write = $lazy_write;
90 10
    }
91
}
92