Skip to content

Commit a485be0

Browse files
Merge branch 'master' into codestyle-cleanup
# Conflicts: # src/Processor/Processor.php
2 parents 3aad3ba + e7616e5 commit a485be0

File tree

5 files changed

+93
-48
lines changed

5 files changed

+93
-48
lines changed

ScriptHandler.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
"autoload": {
1313
"psr-4": {
1414
"EM\\CssCompiler\\": "src/"
15-
},
16-
"classmap": [
17-
"ScriptHandler.php"
18-
]
15+
}
1916
},
2017
"autoload-dev": {
2118
"psr-4": {

src/Processor/Processor.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,47 @@ public function processFile(FileContainer $file)
152152
{
153153
switch ($file->getType()) {
154154
case FileContainer::TYPE_SCSS:
155-
try {
156-
$this->scss->addImportPath(dirname($file->getInputPath()));
157-
$content = $this->scss->compile($file->getInputContent());
158-
159-
return $file->setOutputContent($content);
160-
} catch (ParserException $e) {
161-
throw new CompilerException($e->getMessage(), 1, $e);
162-
}
155+
return $this->compileSCSS($file);
163156
case FileContainer::TYPE_LESS:
164-
try {
165-
return $file->setOutputContent($this->less->compileFile($file->getInputPath()));
166-
} catch (\Exception $e) {
167-
throw new CompilerException($e->getMessage(), 1, $e);
168-
}
157+
return $this->compileLESS($file);
169158
}
170159

171160
throw new CompilerException('unknown compiler');
172161
}
173162

163+
/**
164+
* @param FileContainer $file
165+
*
166+
* @return $this
167+
* @throws CompilerException
168+
*/
169+
protected function compileSCSS(FileContainer $file)
170+
{
171+
try {
172+
$this->scss->addImportPath(dirname($file->getInputPath()));
173+
$content = $this->scss->compile($file->getInputContent());
174+
175+
return $file->setOutputContent($content);
176+
} catch (ParserException $e) {
177+
throw new CompilerException($e->getMessage(), 1, $e);
178+
}
179+
}
180+
181+
/**
182+
* @param FileContainer $file
183+
*
184+
* @return $this
185+
* @throws CompilerException
186+
*/
187+
protected function compileLESS(FileContainer $file)
188+
{
189+
try {
190+
return $file->setOutputContent($this->less->compileFile($file->getInputPath()));
191+
} catch (\Exception $e) {
192+
throw new CompilerException($e->getMessage(), 1, $e);
193+
}
194+
}
195+
174196
/**
175197
* @param string $formatter
176198
*

src/ScriptHandler.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,23 @@ protected static function validateConfiguration(array $config)
6363
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
6464
}
6565

66-
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) {
67-
if (!is_array($el)) {
68-
throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array');
66+
return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
67+
}
68+
69+
/**
70+
* @param array $config
71+
*
72+
* @return bool
73+
* @throws \InvalidArgumentException
74+
*/
75+
protected static function validateOptions(array $config)
76+
{
77+
foreach ($config as $option) {
78+
if (!is_array($option)) {
79+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
6980
}
7081

71-
static::validateOptions($el);
82+
static::validateMandatoryOptions($option);
7283
}
7384

7485
return true;
@@ -80,18 +91,49 @@ protected static function validateConfiguration(array $config)
8091
* @return bool
8192
* @throws \InvalidArgumentException
8293
*/
83-
protected static function validateOptions(array $config)
94+
protected static function validateMandatoryOptions(array $config)
8495
{
8596
foreach (static::$mandatoryOptions as $option) {
8697
if (empty($config[$option])) {
87-
throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!");
98+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
99+
}
100+
101+
switch ($option) {
102+
case static::OPTION_KEY_INPUT:
103+
static::validateIsArray($config[$option]);
104+
break;
105+
case static::OPTION_KEY_OUTPUT:
106+
static::validateIsString($config[$option]);
107+
break;
88108
}
89109
}
90-
if (!is_array($config[static::OPTION_KEY_INPUT])) {
91-
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!');
110+
111+
return true;
112+
}
113+
114+
/**
115+
* @param array $option
116+
*
117+
* @return bool
118+
*/
119+
protected static function validateIsArray($option)
120+
{
121+
if (!is_array($option)) {
122+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
92123
}
93-
if (!is_string($config[static::OPTION_KEY_OUTPUT])) {
94-
throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!');
124+
125+
return true;
126+
}
127+
128+
/**
129+
* @param string $option
130+
*
131+
* @return bool
132+
*/
133+
protected static function validateIsString($option)
134+
{
135+
if (!is_string($option)) {
136+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
95137
}
96138

97139
return true;

tests/phpunit/ScriptHandlerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function validateConfigurationExpectedExceptionOnEmpty()
3232
{
3333
$this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [[ScriptHandler::CONFIG_MAIN_KEY]]);
3434
}
35+
3536
/**
3637
* @see ScriptHandler::validateConfiguration
3738
* @test
@@ -134,10 +135,10 @@ function validateOptionsExpectedExceptionOnOutputNotString()
134135
*/
135136
function validateOptionsOnValid()
136137
{
137-
$result = $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[
138-
ScriptHandler::OPTION_KEY_INPUT => ['string'],
139-
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
140-
]]);
138+
$options = [
139+
[ScriptHandler::OPTION_KEY_INPUT => ['string'], ScriptHandler::OPTION_KEY_OUTPUT => 'string']
140+
];
141+
$result = $this->invokeMethod(new ScriptHandler(), 'validateOptions', [$options]);
141142

142143
$this->assertTrue($result);
143144
}

0 commit comments

Comments
 (0)