Passed
Push — main ( 28d658...6dddcc )
by Michiel
06:12
created

ScssPhpCompiler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 14
eloc 27
c 0
b 0
f 0
dl 0
loc 55
ccs 25
cts 29
cp 0.8621
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkInputFile() 0 13 4
A __construct() 0 8 3
B compile() 0 20 7
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace Phing\Task\Ext\Sass;
24
25
use Exception;
26
use Phing\Exception\BuildException;
27
use Phing\Project;
28
use ScssPhp\ScssPhp\Compiler;
29
use ScssPhp\ScssPhp\OutputStyle;
30
31
class ScssPhpCompiler implements SassTaskCompiler
32
{
33
    /**
34
     * @var Compiler
35
     */
36
    private $scssCompiler;
37
38 4
    public function __construct(string $style, string $loadPath)
39
    {
40 4
        $this->scssCompiler = new Compiler();
41 4
        if ($style) {
42 4
            $this->scssCompiler->setOutputStyle(OutputStyle::fromString(strtolower($style)));
43
        }
44 4
        if ($loadPath !== '') {
45
            $this->scssCompiler->setImportPaths(explode(PATH_SEPARATOR, $loadPath));
46
        }
47
    }
48
49 4
    public function compile(string $inputFilePath, string $outputFilePath, bool $failOnError): void
50
    {
51 4
        if (!$this->checkInputFile($inputFilePath, $failOnError)) {
52 1
            return;
53
        }
54
55
        try {
56 2
            $out = $this->scssCompiler->compileFile($inputFilePath);
57 2
            if ($out !== '') {
0 ignored issues
show
introduced by
The condition $out !== '' is always true.
Loading history...
58 2
                $success = file_put_contents($outputFilePath, $out->getCss());
59 2
                if (!$success && $failOnError) {
0 ignored issues
show
introduced by
The condition $failOnError is always false.
Loading history...
60 2
                    throw new BuildException(
61 2
                        "Cannot write to output file " . var_export($outputFilePath, true),
62 2
                        Project::MSG_INFO
63 2
                    );
64
                }
65
            }
66
        } catch (Exception $ex) {
67
            if ($failOnError) {
0 ignored issues
show
introduced by
The condition $failOnError is always false.
Loading history...
68
                throw new BuildException($ex);
69
            }
70
        }
71
    }
72
73 4
    private function checkInputFile($inputFilePath, $failOnError): bool
74
    {
75 4
        if (file_exists($inputFilePath) && is_readable($inputFilePath)) {
76 2
            return true;
77
        }
78
79 2
        if ($failOnError) {
80 1
            throw new BuildException(
81 1
                "Cannot read from input file " . var_export($inputFilePath, true),
82 1
                Project::MSG_INFO
0 ignored issues
show
Bug introduced by
Phing\Project::MSG_INFO of type integer is incompatible with the type Exception|Phing\Parser\Location|null expected by parameter $p2 of Phing\Exception\BuildException::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                /** @scrutinizer ignore-type */ Project::MSG_INFO
Loading history...
83 1
            );
84
        }
85 1
        return false;
86
    }
87
}
88