Completed
Push — master ( 7bae76...0958d1 )
by Vladimir
9s
created

SassEngine::getSourceMapTargetFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\AssetEngine\Sass;
9
10
use __;
11
use allejo\stakx\AssetEngine\AssetEngineInterface;
12
use allejo\stakx\Configuration;
13
use allejo\stakx\Document\BasePageView;
14
use allejo\stakx\Document\StaticPageView;
15
use allejo\stakx\Filesystem\FilesystemLoader as fs;
16
use allejo\stakx\Manager\PageManager;
17
use allejo\stakx\Service;
18
use Leafo\ScssPhp\Compiler;
19
use Leafo\ScssPhp\Formatter\Compact;
20
use Leafo\ScssPhp\Formatter\Crunched;
21
use Leafo\ScssPhp\Formatter\Expanded;
22
use Leafo\ScssPhp\Formatter\Nested;
23
24
class SassEngine implements AssetEngineInterface
25
{
26
    private $fileSourceMap = false;
27
    private $configuration;
28
    /** @var PageManager */
29
    private $pageManager;
30
    private $compiler;
31
    private $options = [];
32
33 7
    public function __construct(Configuration $configuration)
34
    {
35 7
        $this->configuration = $configuration;
36 7
        $this->compiler = new Compiler();
37 7
    }
38
39 7
    public function getConfigurationNamespace()
40
    {
41 7
        return 'scss';
42
    }
43
44 7
    public function getDefaultConfiguration()
45
    {
46
        return [
47 7
            'style' => 'compressed',
48
            'sourcemap' => false,
49
        ];
50
    }
51
52 7
    public function getFolder()
53
    {
54 7
        return '_sass';
55
    }
56
57 7
    public function getExtensions()
58
    {
59 7
        return ['scss'];
60
    }
61
62
    /**
63
     * @param string $content
64
     * @param $options = [
65
     *     'pageview' => new StaticPageView()
66
     * ]
67
     *
68
     * @return string
69
     */
70 7
    public function parse($content, array $options = [])
71
    {
72
        $sourceMapOptions = [
73 7
            'sourceMapBasepath' => Service::getWorkingDirectory(),
74
        ];
75
76
        // We don't need to write the source map to a file
77 7
        if (!$this->fileSourceMap)
78
        {
79 6
            $this->compiler->setSourceMapOptions($sourceMapOptions);
80
81 6
            return $this->compiler->compile($content);
82
        }
83
84
        /** @var StaticPageView $pageView */
85 1
        $pageView = $options['pageview'];
86
87
        // Always put our source map next to the respective CSS file
88 1
        $sourceMapTargetPath = $this->getSourceMapTargetFile($pageView);
89 1
        $sourceMapFilename = fs::getFilename($sourceMapTargetPath);
90
91 1
        $sourceMapOptions = array_merge($sourceMapOptions, [
92 1
            'sourceMapFilename' => $sourceMapFilename,
93 1
            'sourceMapURL' => $pageView->getPermalink() . '.map',
94 1
            'sourceMapWriteTo' => $sourceMapTargetPath,
95
        ]);
96
97 1
        $sourceMapGenerator = new SourceMapGenerator($sourceMapOptions);
98 1
        $this->compiler->setSourceMap($sourceMapGenerator);
0 ignored issues
show
Documentation introduced by
$sourceMapGenerator is of type object<allejo\stakx\Asse...ass\SourceMapGenerator>, but the function expects a integer.

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...
99
100 1
        $sass = $this->compiler->compile($content);
101
102 1
        $sourceMapPageView = BasePageView::createVirtual([
103 1
            'permalink' => $pageView->getPermalink() . '.map',
104 1
        ], $sourceMapGenerator->getSourceMapContents());
105
106 1
        $this->pageManager->trackNewPageView($sourceMapPageView);
107
108 1
        return $sass;
109
    }
110
111 7
    public function setOptions(array $options)
112
    {
113 7
        $this->options = $options;
114
115 7
        $this->configureImportPath();
116 7
        $this->configureOutputStyle();
117 7
        $this->configureSourceMap();
118 7
    }
119
120 1
    public function setPageManager(PageManager $pageManager)
121
    {
122 1
        $this->pageManager = $pageManager;
123 1
    }
124
125 7
    private function configureImportPath()
126
    {
127 7
        $this->compiler->setImportPaths(Service::getWorkingDirectory() . '/_sass/');
128 7
    }
129
130 7
    private function configureOutputStyle()
131
    {
132 7
        $style = __::get($this->options, 'style', 'compressed');
133
134 7
        $this->compiler->setFormatter(self::stringToFormatter($style));
135 7
    }
136
137 7
    private function configureSourceMap()
138
    {
139 7
        $sourceMap = __::get($this->options, 'sourcemap');
140
141 7
        if ($sourceMap === 'inline') {
142 1
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_INLINE);
143
        }
144 6
        elseif ($sourceMap === true) {
145 1
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_FILE);
146 1
            $this->fileSourceMap = true;
147
        }
148
        else {
149 5
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_NONE);
150
        }
151 7
    }
152
153 1
    private function getSourceMapTargetFile(StaticPageView $pageView)
154
    {
155 1
        return fs::absolutePath(
156 1
            $this->configuration->getTargetFolder(),
0 ignored issues
show
Documentation introduced by
$this->configuration->getTargetFolder() is of type string, but the function expects a object<string>.

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...
157 1
            $pageView->getTargetFile() . '.map'
0 ignored issues
show
Unused Code introduced by
The call to FilesystemLoader::absolutePath() has too many arguments starting with $pageView->getTargetFile() . '.map'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
158
        );
159
    }
160
161 7
    public static function stringToFormatter($format)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
162
    {
163 7
        if ($format == 'nested')
164
        {
165 1
            return Nested::class;
166
        }
167 6
        elseif ($format == 'expanded')
168
        {
169 1
            return Expanded::class;
170
        }
171 5
        elseif ($format == 'compact')
172
        {
173 1
            return Compact::class;
174
        }
175
176 4
        return Crunched::class;
177
    }
178
}
179