Completed
Pull Request — master (#10)
by Tomáš
03:17
created

FileSource::init()   C

Complexity

Conditions 11
Paths 42

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 13.0563

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 26
cts 35
cp 0.7429
rs 5.8904
c 0
b 0
f 0
cc 11
eloc 39
nc 42
nop 1
crap 13.0563

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
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Source;
13
14
use Symfony\Component\Finder\SplFileInfo;
15
use Dflydev\Canal\Analyzer\Analyzer;
16
use Dflydev\DotAccessConfiguration\Configuration as Data;
17
use Dflydev\DotAccessConfiguration\YamlConfigurationBuilder as YamlDataBuilder;
18
19
final class FileSource extends AbstractSource
20
{
21
    /**
22
     * Constructor.
23
     *
24
     * @param Analyzer            $analyzer   Analyzer
25
     * @param DataSourceInterface $dataSource Data Source
26
     * @param SplFileInfo         $file       File
27
     * @param bool                $isRaw      Should be treated as raw
28
     * @param bool                $hasChanged Has the file changed?
29
     */
30 6
    public function __construct(Analyzer $analyzer, DataSourceInterface $dataSource, SplFileInfo $file, $isRaw, $hasChanged = false)
31
    {
32 6
        $this->analyzer = $analyzer;
0 ignored issues
show
Bug introduced by
The property analyzer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33 6
        $this->sourceId = 'FileSource:'.$dataSource->dataSourceId().':'.$file->getRelativePathname();
34 6
        $this->relativePathname = $file->getRelativePathname();
35 6
        $this->filename = $file->getFilename();
36 6
        $this->file = $file;
37 6
        $this->isRaw = $isRaw;
38 6
        $this->hasChanged = $hasChanged;
39
40 6
        $internetMediaTypeFactory = $this->analyzer->getInternetMediaTypeFactory();
41 6
        $this->applicationXmlType = $internetMediaTypeFactory->createApplicationXml();
0 ignored issues
show
Bug introduced by
The property applicationXmlType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
43 6
        $this->init();
44 6
    }
45
46
    /**
47
     * Initialize source.
48
     *
49
     * @param bool $hasChanged Has the file changed?
50
     */
51 6
    protected function init($hasChanged = null)
52
    {
53 6
        parent::init($hasChanged);
54
55 6
        $originalData = $this->data;
56
57 6
        if ($this->isRaw) {
58
            $this->useFileReference = true;
59
            $this->data = new Data();
60
        } else {
61 6
            $internetMediaType = $this->analyzer->detectFromFilename($this->file);
62
63 6
            if ($internetMediaType &&
64 6
                ('text' === $internetMediaType->getType() ||
65 6
                $this->applicationXmlType->equals($internetMediaType))) {
66
                // Only text files can be processed by Sculpin and since we
67
                // have to read them here we are going to ensure that we use
68
                // the content we read here instead of having someone else
69
                // read the file again later.
70 6
                $this->useFileReference = false;
71
72
                // Additionally, any text file is a candidate for formatting.
73 6
                $this->canBeFormatted = true;
74
75 6
                $content = file_get_contents($this->file);
76
77 6
                if (preg_match('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', $content, $matches)) {
78 4
                    $this->content = $matches[2];
79 4
                    if (preg_match('/^(\s*[-]+\s*|\s*)$/', $matches[1])) {
80
                        // There is nothing useful in the YAML front matter.
81
                        $this->data = new Data();
82
                    } else {
83
                        // There may be YAML frontmatter
84
                        try {
85 4
                            $builder = new YamlDataBuilder($matches[1]);
86 4
                            $this->data = $builder->build();
87 3
                        } catch (\InvalidArgumentException $e) {
88
                            // Likely not actually YAML front matter available,
89
                            // treat the entire file as pure content.
90 3
                            echo ' ! '.$this->sourceId().' '.$e->getMessage().' !'.PHP_EOL;
91 3
                            $this->content = $content;
92 4
                            $this->data = new Data();
93
                        }
94
                    }
95
                } else {
96 2
                    $this->content = $content;
97 2
                    $this->data = new Data();
98 6
                    $this->canBeFormatted = false;
99
                }
100
            } else {
101
                $this->useFileReference = true;
102
                $this->data = new Data();
103
            }
104
        }
105
106 6
        if ($this->data->get('date')) {
107
            if (!is_numeric($this->data->get('date'))) {
108
                $this->data->set('date', strtotime($this->data->get('date')));
109
            }
110
111
            $this->data->set('calculated_date', $this->data->get('date'));
112
        }
113
114 6
        if ($originalData) {
115
            $this->data->import($originalData, false);
116
        }
117 6
    }
118
}
119