Completed
Pull Request — master (#3)
by Harry
05:31
created

FormatterFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B getFormatter() 0 30 5
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Formatter;
15
16
use Graze\DataFile\Format\CsvFormatInterface;
17
use Graze\DataFile\Format\FormatInterface;
18
use Graze\DataFile\Format\JsonFormatInterface;
19
use InvalidArgumentException;
20
21
class FormatterFactory implements FormatterFactoryInterface
22
{
23
    /**
24
     * @param FormatInterface $format
25
     *
26
     * @return FormatterInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be CsvFormatter|null|JsonFormatter?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28 5
    public function getFormatter(FormatInterface $format)
29
    {
30 5
        switch ($format->getType()) {
31 5
            case 'csv':
32 2
                if ($format instanceof CsvFormatInterface) {
33 1
                    return new CsvFormatter($format);
34
                } else {
35 1
                    throw new InvalidArgumentException(
36
                        "Format indicates it is csv but does not implement CsvFormatInterface"
37 1
                    );
38
                }
39
40
                // this will never be called but phpcf doesn't understand the concept of exceptions
41
                break;
0 ignored issues
show
Unused Code introduced by
// this will never be ca...pt of exceptions break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
42
43 3
            case 'json':
44 2
                if ($format instanceof JsonFormatInterface) {
45 1
                    return new JsonFormatter($format);
46
                } else {
47 1
                    throw new InvalidArgumentexception(
48
                        "Format indicates it is json but does not implement JsonFormatInterface"
49 1
                    );
50
                }
51
52
                // this will never be called but phpcf doesn't understand the concept of exceptions
53
                break;
0 ignored issues
show
Unused Code introduced by
// this will never be ca...pt of exceptions break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
54 1
            default:
55 1
                throw new InvalidArgumentException("Supplied format: {$format->getType()} is unknown");
56 1
        }
57
    }
58
}
59