LocalFileXrefResolver::getAbsoluteLocation()   C
last analyzed

Complexity

Conditions 16
Paths 20

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 134.7953

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 7
cts 31
cp 0.2258
rs 5.9237
c 0
b 0
f 0
cc 16
eloc 30
nc 20
nop 2
crap 134.7953

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ConfigToken\TreeCompiler\XrefResolver\Types;
4
5
use ConfigToken\TreeCompiler\XrefResolver\Exception\XrefResolverFetchException;
6
use ConfigToken\TreeCompiler\Xref;
7
use ConfigToken\TreeSerializer\Exception\TreeSerializerSyntaxException;
8
use ConfigToken\TreeSerializer\TreeSerializerFactory;
9
10
11
class LocalFileXrefResolver extends AbstractXrefResolver
12
{
13
    /**
14
     * Get the resolver type identifier string.
15
     *
16
     * @return string
17
     */
18 1
    public static function getType()
19
    {
20 1
        return 'file';
21
    }
22
23
    /**
24
     * Get the absolute location for the given Xref based on the specified include path.
25
     *
26
     * @param string $xrefLocation The location.
27
     * @param Xref[] $xrefPath Inclusion path.
28
     * @return string
29
     */
30 7
    public static function getAbsoluteLocation($xrefLocation, $xrefPath)
31
    {
32 7
        $xrefLocation = static::getPlatformSpecificLocation($xrefLocation);
33
        // no inclusion path, return absolute location or as set
34 7
        if ((!isset($xrefPath)) || (!is_array($xrefPath)) || (count($xrefPath) == 0)) {
35
            $dirname = pathinfo($xrefLocation, PATHINFO_DIRNAME);
36
            if ($dirname == '.') {
37
                return getcwd() . DIRECTORY_SEPARATOR . $xrefLocation;
38
            }
39
            return $xrefLocation;
40
        }
41
42 7
        $xrefLocationLen = strlen($xrefLocation);
43
44
        // empty location
45 7
        if ($xrefLocationLen == 0) {
46
            return $xrefLocation;
47
        }
48
49
        // absolute location
50 7
        if (($xrefLocation[0] == DIRECTORY_SEPARATOR) || (($xrefLocationLen >= 2) && ($xrefLocation[1] == ':'))) {
51 7
            return $xrefLocation;
52
        }
53
54
        // relative location
55
        while (count($xrefPath) > 0) {
56
            $prevXref = array_pop($xrefPath);
57
            if (!($prevXref instanceof Xref) || ($prevXref->getType() != static::getType())) {
58
                continue;
59
            }
60
            $prevXrefLocation = static::getAbsoluteLocation($prevXref->getLocation(), $xrefPath);
61
            $prevXrefPath = pathinfo($prevXrefLocation, PATHINFO_DIRNAME);
62
            if ($prevXrefPath == '.') {
63
                $prevXrefPath = null;
64
                break;
65
            }
66
            if (strlen($prevXrefPath) == 0) {
67
                $prevXrefPath = null;
68
                continue;
69
            }
70
            break;
71
        }
72
73
        if (!isset($prevXrefPath)) {
74
            $prevXrefPath = getcwd();
75
        }
76
77
        $absoluteXrefLocation = $prevXrefPath . DIRECTORY_SEPARATOR . $xrefLocation;
78
        $realXrefLocation = realpath($absoluteXrefLocation);
79
80
        return $realXrefLocation === false ? $absoluteXrefLocation : $realXrefLocation;
81
    }
82
83
    /**
84
     * Return a platform specific location string derived from the given location string.
85
     *
86
     * @param $xrefLocation
87
     * @return mixed
88
     */
89 7
    public static function getPlatformSpecificLocation($xrefLocation)
90
    {
91 7
        return str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $xrefLocation);
92
    }
93
94
    /**
95
     * Fetch the data from the specified location of the Xref.
96
     *
97
     * @param Xref $xref
98
     * @param boolean $force If true and Xref already fetched, force the resolver to fetch the data again.
99
     * @throws XrefResolverFetchException
100
     */
101
    public static function resolve(Xref $xref, $force = false)
102
    {
103
        if ($xref->isResolved() && (!$force)) {
104
            return;
105
        }
106
        static::matchType($xref);
107
        if (!$xref->hasLocation()) {
108
            throw new XrefResolverFetchException($xref);
109
        }
110
        $xrefLocation = $xref->getLocation();
111
        if (!file_exists($xrefLocation)) {
112
            throw new XrefResolverFetchException($xref, 'File does not exist.');
113
        }
114
        try {
115
            $data = file_get_contents($xrefLocation);
116
        } catch (\Exception $e) {
117
            throw new XrefResolverFetchException($xref, $e->getMessage());
118
        }
119
        $xref->setResolved(false);
120
        if ($xref->hasContentType()) {
121
            $serializer = TreeSerializerFactory::getByContentType($xref->getContentType());
122
        } else {
123
            $fileExtension = pathinfo($xrefLocation, PATHINFO_EXTENSION);
124
            $serializer = TreeSerializerFactory::getByFileExtension($fileExtension);
125
            $xref->setContentType($serializer::getContentType());
126
        }
127
        try {
128
            $data = $serializer::deserialize($data);
129
        } catch (TreeSerializerSyntaxException $e) {
130
            throw new XrefResolverFetchException($xref, $e->getMessage());
131
        }
132
        $xref->setData($data);
133
        $xref->setResolved(true);
134
    }
135
}