Completed
Pull Request — refactor-01-decoupling (#27)
by John
02:14
created

FieldParserFactory::getFieldParser()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 43
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 31
nc 8
nop 3
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldParserRegistry;
6
use Graze\CiffRenderer\Parser\FieldType;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFixedText;
8
use Graze\CiffRenderer\Parser\FieldParser\FieldParserBarcode;
9
use Graze\CiffRenderer\Parser\FieldParser\FieldParserDate\FieldParserDate;
10
use Graze\CiffRenderer\Parser\FieldParser\FieldParserDate\FieldParserDateOffset;
11
use Graze\CiffRenderer\Parser\FieldParser\FieldParserStaticGraphic;
12
use Graze\CiffRenderer\Parser\FieldParser\FieldParserGraphicPrimitive;
13
use Graze\CiffRenderer\Exception\UnsupportedFieldTypeException;
14
15
class FieldParserFactory
16
{
17
    /**
18
     * @var FieldParserRegistry
19
     */
20
    private $fieldParserRegistry;
21
22
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$fieldParserRegistry" missing
Loading history...
23
     * @param FieldParserRegistry $fieldParserFactory
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldParserFactory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
Coding Style introduced by
Doc comment for parameter $fieldParserFactory does not match actual variable name $fieldParserRegistry
Loading history...
24
     */
25
    public function __construct(FieldParserRegistry $fieldParserRegistry)
26
    {
27
        $this->fieldParserRegistry = $fieldParserRegistry;
28
    }
29
30
    /**
31
     * @param SimpleXMLElement $xmlField
0 ignored issues
show
Documentation introduced by
Should the type for parameter $xmlField not be \SimpleXMLElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
     * @param SimpleXMLElement $xmlHeader
0 ignored issues
show
Documentation introduced by
Should the type for parameter $xmlHeader not be \SimpleXMLElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
33
     * @param float $scale
34
     * @return Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface
35
     */
36
    public function getFieldParser(\SimpleXMLElement $xmlField, \SimpleXMLElement $xmlHeader, $scale)
0 ignored issues
show
Coding Style introduced by
Expected type hint "SimpleXMLElement"; found "\SimpleXMLElement" for $xmlField
Loading history...
Coding Style introduced by
Expected type hint "SimpleXMLElement"; found "\SimpleXMLElement" for $xmlHeader
Loading history...
37
    {
38
        switch ((string) $xmlField->FldType) { // @codingStandardsIgnoreLine
39
            case FieldType::FIELD_FIXED_TEXT:
40
            case FieldType::FIELD_MERGE_FIELD:
41
                $className = FieldParserFixedText::class;
42
                break;
43
44
            case FieldType::FIELD_BARCODE:
45
                $className = FieldParserBarcode::class;
46
                break;
47
48
            case FieldType::FIELD_DATE:
49
                $className = FieldParserDate::class;
50
                break;
51
52
            case FieldType::FIELD_OFFSET_DATE:
53
                $className = FieldParserDateOffset::class;
54
                break;
55
56
            case FieldType::FIELD_STATIC_GRAPHIC:
57
                $className = FieldParserStaticGraphic::class;
58
                break;
59
60
            case FieldType::FIELD_GRAPHIC_PRIMITIVE:
61
                $className = FieldParserGraphicPrimitive::class;
62
                break;
63
64
            default:
65
                throw new UnsupportedFieldTypeException($fieldType);
66
                break;
0 ignored issues
show
Unused Code introduced by
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...
67
        }
68
69
        $parser = $className::factory();
70
        $parser->setXmlField($xmlField);
71
        $parser->setXmlHeader($xmlHeader);
72
        $parser->setScale($scale);
73
        $parser->setFieldParserRegistry($this->fieldParserRegistry);
74
75
        $this->fieldParserRegistry->addParser($parser);
76
77
        return $parser;
78
    }
79
80
    /**
81
     * @return FieldParserFactory
82
     */
83
    public static function factory()
84
    {
85
        return new static(
86
            new FieldParserRegistry()
87
        );
88
    }
89
}
90