Completed
Pull Request — master (#14)
by Tomáš
03:35
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
20
{
21
22
23
    protected function init(bool $hasChanged = null)
24
    {
25
    $content = file_get_contents($this->file);
26
27
    // extract data from file!
28
    if (preg_match('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', $content, $matches)) {
29
        $this->content = $matches[2];
30
        if (preg_match('/^(\s*[-]+\s*|\s*)$/', $matches[1])) {
31 6
            // There is nothing useful in the YAML front matter.
32
            $this->data = new Data();
33
        } else {
34
            // There may be YAML frontmatter
35
            try {
36
                $builder = new YamlDataBuilder($matches[1]);
37
                $this->data = $builder->build();
38 6
            } catch (\InvalidArgumentException $e) {
39 6
                // Likely not actually YAML front matter available,
40 6
                // treat the entire file as pure content.
41 6
                echo ' ! '.$this->sourceId().' '.$e->getMessage().' !'.PHP_EOL;
42 6
                $this->content = $content;
43 6
                $this->data = new Data();
44 6
            }
45
        }
46 6
    } else {
47 6
        $this->content = $content;
48
        $this->data = new Data();
49 6
        $this->canBeFormatted = false;
50 6
}
51
}
52
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected EOF, expecting T_FUNCTION
Loading history...