Completed
Push — master ( 46dc9d...2f0783 )
by Markus
03:36
created

ServiceStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 9.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 68.42%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 9
c 7
b 0
f 1
lcom 1
cbo 3
dl 9
loc 94
ccs 26
cts 38
cp 0.6842
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getCallable() 0 4 1
A setObjectFactory() 0 4 1
A setObjectTransformer() 0 4 1
A reader() 0 10 1
A writer() 0 9 1
A info() 9 9 1
A getFields() 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
namespace Mathielen\ImportEngine\Storage;
3
4
use Mathielen\DataImport\Reader\ServiceReader;
5
use Mathielen\DataImport\Writer\ServiceWriter;
6
7
class ServiceStorage implements StorageInterface
8
{
9
10
    /**
11
     * @var callable
12
     */
13
    private $callable;
14
15
    /**
16
     * @var array
17
     */
18
    private $arguments;
19
20
    private $objectTransformer;
21
    private $objectFactory;
22
23 3
    public function __construct(callable $callable, $arguments=array(), $objectMapper=null)
24
    {
25 3
        $this->callable = $callable;
26 3
        $this->arguments = $arguments;
27 3
        $this->setObjectFactory($objectMapper);
28 3
        $this->setObjectTransformer($objectMapper);
29
30 3
        if (!is_callable($callable)) {
31
            throw new \InvalidArgumentException("Given callable is not a callable");
32
        }
33 3
    }
34
35
    /**
36
     * @return callable
37
     */
38
    public function getCallable()
39
    {
40
        return $this->callable;
41
    }
42
43 3
    public function setObjectFactory($objectFactory)
44
    {
45 3
        $this->objectFactory = $objectFactory;
46 3
    }
47
48 3
    public function setObjectTransformer($objectTransformer)
49
    {
50 3
        $this->objectTransformer = $objectTransformer;
51 3
    }
52
53
    /*
54
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
55
     */
56 1
    public function reader()
57
    {
58 1
        $reader = new ServiceReader(
59 1
            $this->callable,
60 1
            $this->arguments,
61 1
            $this->objectTransformer
0 ignored issues
show
Unused Code introduced by
The call to ServiceReader::__construct() has too many arguments starting with $this->objectTransformer.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62 1
        );
63
64 1
        return $reader;
65
    }
66
67
    /*
68
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
69
     */
70 1
    public function writer()
71
    {
72 1
        $writer = new ServiceWriter(
73 1
            $this->callable,
74 1
            $this->objectFactory
75 1
        );
76
77 1
        return $writer;
78
    }
79
80
    /*
81
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
82
     */
83 View Code Duplication
    public function info()
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...
84
    {
85
        return new StorageInfo(array(
86
            'name' => $this->callable.'',
87
            'format' => 'Service method',
88
            'size' => 0,
89
            'count' => count($this->reader())
90
        ));
91
    }
92
93
    /*
94
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields()
95
     */
96
    public function getFields()
97
    {
98
        return $this->reader()->getFields();
99
    }
100
}
101