CompassFilter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 107
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 30 4
A checkRequirements() 0 9 2
A getSupportedExtensions() 0 6 1
A getBin() 0 6 1
A buildCommandString() 0 20 1
1
<?php
2
3
namespace JK\Sam\Filter\Compass;
4
5
use Exception;
6
use JK\Sam\Filter\Filter;
7
use SplFileInfo;
8
use Symfony\Component\Process\Process;
9
10
class CompassFilter extends Filter
11
{
12
    /**
13
     * Compile scss file into css file via compass.
14
     *
15
     * @param SplFileInfo[] $sources
16
     * @param SplFileInfo[] $destinations
17
     * @return SplFileInfo[]
18
     * @throws Exception
19
     */
20
    public function run(array $sources, array $destinations)
21
    {
22
        $updatedSources = [];
23
24
        foreach ($sources as $source) {
25
            // remove .scss extension and add .css extension
26
            $css = $this->getCacheDir().$source->getBasename('.scss').'.css';
27
            $this->addNotification('compiling '.$source.' to '.$css);
28
29
            // compilation start
30
            $process = new Process($command = $this->buildCommandString($source));
0 ignored issues
show
Documentation introduced by
$command = $this->buildCommandString($source) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
            $process->run();
32
33
            if (!$process->isSuccessful()) {
34
                throw new Exception(
35
                    'An error has occurred during the compass compile task : "'.$process->getErrorOutput().""
36
                    .' (running "'.$command.'")'
37
                );
38
            }
39
40
            // add css file to destination files
41
            $updatedSources[] = $css;
42
        }
43
        if (count($updatedSources)) {
44
            // compilation success notification
45
            $this->addNotification('scss files compilation success');
46
        }
47
48
        return $updatedSources;
49
    }
50
51
    /**
52
     * Check that compass is installed and available.
53
     * 
54
     * @throws Exception
55
     */
56
    public function checkRequirements()
57
    {
58
        $process = new Process($this->getBin().' version');
0 ignored issues
show
Documentation introduced by
$this->getBin() . ' version' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $process->run();
60
61
        if (!$process->isSuccessful()) {
62
            throw new Exception('compass is not found at '.$this->getBin().'. '.$process->getErrorOutput());
63
        }
64
    }
65
66
    /**
67
     * Return the file extensions supported by this filter.
68
     *
69
     * @return string[]
70
     */
71
    public function getSupportedExtensions()
72
    {
73
        return [
74
            'scss'
75
        ];
76
    }
77
78
    /**
79
     * Get the binary path.
80
     *
81
     * @return string
82
     */
83
    protected function getBin()
84
    {
85
        return $this
86
            ->configuration
87
            ->getParameter('bin');
88
    }
89
90
    /**
91
     * Build the compass compile command line.
92
     *
93
     * @param SplFileInfo $source
94
     * @return string
95
     */
96
    protected function buildCommandString($source)
97
    {
98
        $commandPattern = '%s %s %s %s %s';
99
        $command = sprintf(
100
        // pattern
101
            $commandPattern,
102
            // compass binary path
103
            $this->getBin(),
104
            // compass command
105
            'compile',
106
            // sources
107
            $source->getRealPath(),
108
            // destination file
109
            '--css-dir='.$this->getCacheDir(),
110
            // sass directory
111
            '--sass-dir='.$source->getPath()
112
        );
113
114
        return $command;
115
    }
116
}
117