Completed
Push — master ( a3f369...f825b4 )
by Tim
14s
created

Interpreter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 24
loc 150
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A interpret() 0 9 1
A unstrict() 0 4 1
A addObserver() 0 4 1
A reset() 0 5 1
A isNotStrict() 0 4 1
A notify() 0 11 2
A delegate() 0 4 1
B checkRowConsistency() 24 24 4

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
/**
4
 * TechDivision\Import\Adapter\Goodby\Interpreter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Adapter\Goodby;
22
23
use Goodby\CSV\Import\Protocol\InterpreterInterface;
24
use Goodby\CSV\Import\Protocol\Exception\InvalidLexicalException;
25
use Goodby\CSV\Import\Standard\Exception\StrictViolationException;
26
27
/**
28
 * Custom interpreter implementation which allows to reset observers and row consistency on every import.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class Interpreter implements InterpreterInterface
37
{
38
39
    /**
40
     * The array with the observers.
41
     *
42
     * @var array
43
     */
44
    private $observers = array();
45
46
    /**
47
     * The number of rows found in the header.
48
     *
49
     * @var int
50
     */
51
    private $rowConsistency = null;
52
53
    /**
54
     * Query whether or not strict mode is activated or not.
55
     *
56
     * @var boolean
57
     */
58
    private $strict = true;
59
60
    /**
61
     * Interpret the passed line.
62
     *
63
     * @param array $row The row that has to be processed
64
     *
65
     * @return void
66
     * @throws \Goodby\CSV\Import\Protocol\Exception\InvalidLexicalException Is thrown, if the passed line is not an array
67
     */
68
    public function interpret($row)
69
    {
70
71
        // check the row consistency
72
        $this->checkRowConsistency($row);
73
74
        // invoke the observers
75
        $this->notify($row);
76
    }
77
78
    /**
79
     * Disable strict mode.
80
     *
81
     * @return void
82
     */
83
    public function unstrict()
84
    {
85
        $this->strict = false;
86
    }
87
88
    /**
89
     * Add's the passed observer to the interpreter.
90
     *
91
     * @param callable $observer The observer to add
92
     *
93
     * @return void
94
     */
95
    public function addObserver(callable $observer)
96
    {
97
        $this->observers[] = $observer;
98
    }
99
100
    /**
101
     * Reset the interpreter.
102
     *
103
     * @return void
104
     */
105
    public function reset()
106
    {
107
        $this->observers = array();
108
        $this->rowConsistency = null;
109
    }
110
111
    /**
112
     * Query whether or not strict mode has been activated.
113
     *
114
     * @return boolean TRUE if the strict mode has NOT been activated, else FALSE
115
     */
116
    private function isNotStrict()
117
    {
118
        return $this->strict === false;
119
    }
120
121
    /**
122
     * Notify the observers.
123
     *
124
     * @param array $row The row that has to be processed
125
     *
126
     * @return void
127
     */
128
    private function notify(array $row)
129
    {
130
131
        // make the observers local
132
        $observers = $this->observers;
133
134
        // invoke the observers on the passed line
135
        foreach ($observers as $observer) {
136
            $this->delegate($observer, $row);
137
        }
138
    }
139
140
    /**
141
     * Delegate the row to the observer.
142
     *
143
     * @param callable $observer The observer that has to be invoked
144
     * @param array    $row      The row that has to be processed
145
     *
146
     * @return void
147
     */
148
    private function delegate(callable $observer, array $row)
149
    {
150
        call_user_func($observer, $row);
151
    }
152
153
    /**
154
     * Check if the column count is consistent with comparing other rows.
155
     *
156
     * @param array $row The row that has to be processed
157
     *
158
     * @return void
159
     * @throws \Goodby\CSV\Export\Standard\Exception\StrictViolationException Is thrown, if row consistency check fails
160
     */
161 View Code Duplication
    private function checkRowConsistency(array $row)
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...
162
    {
163
164
        // query whether or not strict mode is enabled
165
        if ($this->isNotStrict()) {
166
            return;
167
        }
168
169
        // count the number of columns
170
        $current = count($row);
171
172
        // if the row consistency has not been set, set it
173
        if ($this->rowConsistency === null) {
174
            $this->rowConsistency = $current;
175
        }
176
177
        // check row consistency
178
        if ($current !== $this->rowConsistency) {
179
            throw new StrictViolationException(sprintf('Column size should be %u, but %u columns given', $this->rowConsistency, $current));
180
        }
181
182
        // set the new row consistency
183
        $this->rowConsistency = $current;
184
    }
185
}
186