Completed
Push — refactor-04-parser-tests ( 553ee6...bce951 )
by John
03:05 queued 47s
created

ParserDocument::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldSorter;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFactory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Graze\CiffRenderer\Parser\FieldParserFactory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type Graze\CiffRenderer\Parse...rser\FieldParserFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Graze\CiffRenderer\Parser\FieldParserRegistry;
8
use Graze\CiffRenderer\SimpleXmlElement\SimpleXmlElementInterface;
9
10
class ParserDocument
11
{
12
    /**
13
     * @var FieldSorter
14
     */
15
    private $fieldSorter;
16
17
    /**
18
     * @var FieldParserFactory
19
     */
20
    private $fieldParserFactory;
21
22
    /**
23
     * @var FieldParserRegistry
24
     */
25
    private $fieldParserRegistry;
26
27
    /**
28
     * @param FieldSorter $fieldSorter
29
     * @param FieldParserFactory $fieldParserFactory
30
     * @param FieldParserRegistry $fieldParserRegistry
31
     */
32 1
    public function __construct(
33
        FieldSorter $fieldSorter,
34
        FieldParserFactory $fieldParserFactory,
35
        FieldParserRegistry $fieldParserRegistry
36
    ) {
37 1
        $this->fieldSorter = $fieldSorter;
38 1
        $this->fieldParserFactory = $fieldParserFactory;
39 1
        $this->fieldParserRegistry = $fieldParserRegistry;
40 1
    }
41
42
    /**
43
     * @param SimpleXmlElementInterface $xml
44
     * @return ParserInterface[]
45
     */
46
    public function getFieldParsers(SimpleXmlElementInterface $xml)
47
    {
48
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Simpl...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
        $fields = $subImage['Field'];
50
        if (!is_array($fields)) {
51
            // ensure we have an array even if only a single field is present
52
            $fields = [$fields];
53
        }
54
55
        $this->fieldSorter->sort($fields);
56
57
        $parsers = [];
58
        foreach ($fields as $field) {
59
            $parser = $this->fieldParserFactory->getFieldParser($field);
60
            // @codingStandardsIgnoreLine
61
            $parser->parse($this->fieldParserRegistry, $field, $xml->Header, $this->getScale($xml));
0 ignored issues
show
Bug introduced by
Accessing Header on the interface Graze\CiffRenderer\Simpl...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
            $parsers[] = $parser;
63
        }
64
65
        return $parsers;
66
    }
67
68
    /**
69
     * @param SimpleXmlElementInterface $xml
70
     * @return float
71
     */
72
    public function getWidth(SimpleXmlElementInterface $xml)
73
    {
74
        // @codingStandardsIgnoreLine
75
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Simpl...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
76
    }
77
78
    /**
79
     * @param SimpleXmlElementInterface $xml
80
     * @return float
81
     */
82
    public function getHeight(SimpleXmlElementInterface $xml)
83
    {
84
        // @codingStandardsIgnoreLine
85
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Simpl...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
86
    }
87
88
    /**
89
     * @param SimpleXmlElementInterface $xml
90
     * @return float
91
     */
92
    public function getScale(SimpleXmlElementInterface $xml)
93
    {
94
        // @codingStandardsIgnoreLine
95
        return (float) $xml->SubImage->SubHeader->XRes;
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Simpl...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
96
    }
97
98
    /**
99
     * @return ParserDocument
100
     */
101
    public static function factory()
102
    {
103
        return new static(
104
            new FieldSorter(),
105
            new FieldParserFactory(),
106
            new FieldParserRegistry()
107
        );
108
    }
109
}
110