Completed
Push — master ( 85851c...4b020b )
by Harry
03:30
created

ParserFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 3
dl 35
loc 35
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getParser() 27 27 5

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
namespace Graze\DataFile\Format\Parser;
4
5
use Graze\DataFile\Format\CsvFormatInterface;
6
use Graze\DataFile\Format\FormatAwareInterface;
7
use Graze\DataFile\Format\FormatInterface;
8
use Graze\DataFile\Format\JsonFormatInterface;
9
use Graze\DataFile\Node\FileNodeInterface;
10
use InvalidArgumentException;
11
12 View Code Duplication
class ParserFactory implements ParserFactoryInterface
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * @param FormatInterface $format
16
     *
17
     * @return ParserInterface
18
     */
19 8
    public function getParser(FormatInterface $format)
20
    {
21 8
        switch ($format->getType()) {
22 8
            case 'csv':
23 4
                if ($format instanceof CsvFormatInterface) {
24 3
                    return new CsvParser($format);
25
                } else {
26 1
                    throw new InvalidArgumentException(
27 1
                        "Format indicates it is csv but does not implement CsvFormatInterface"
28
                    );
29
                }
30
31
            // fallthrough
32 4
            case 'json':
33 3
                if ($format instanceof JsonFormatInterface) {
34 2
                    return new JsonParser($format);
35
                } else {
36 1
                    throw new InvalidArgumentexception(
37 1
                        "Format indicates it is json but does not implement JsonFormatInterface"
38
                    );
39
                }
40
41
            // fallthrough
42
            default:
43 1
                throw new InvalidArgumentException("Supplied format: {$format->getType()} is unknown");
44
        }
45
    }
46
}
47