Skip to content

Commit 30c19fd

Browse files
improved composer handler
1 parent 0583d11 commit 30c19fd

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/ScriptHandler.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class ScriptHandler
1818
const OPTION_KEY_FORMATTER = 'format';
1919
const DEFAULT_OPTION_FORMATTER = 'compact';
2020
protected static $mandatoryOptions = [
21-
self::OPTION_KEY_INPUT,
22-
self::OPTION_KEY_OUTPUT
21+
self::OPTION_KEY_INPUT => 'array',
22+
self::OPTION_KEY_OUTPUT => 'string'
2323
];
2424

2525
/**
26+
* @api
27+
*
2628
* @param Event $event
2729
*
2830
* @throws \InvalidArgumentException
@@ -34,15 +36,15 @@ public static function generateCSS(Event $event)
3436

3537
$processor = new Processor($event->getIO());
3638

37-
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
38-
foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) {
39+
foreach ($extra[static::CONFIG_MAIN_KEY] as $options) {
40+
foreach ($options[static::OPTION_KEY_INPUT] as $inputSource) {
3941
$processor->attachFiles(
4042
static::resolvePath($inputSource, getcwd()),
41-
static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd())
43+
static::resolvePath($options[static::OPTION_KEY_OUTPUT], getcwd())
4244
);
4345
}
4446

45-
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
47+
$formatter = array_key_exists(static::OPTION_KEY_FORMATTER, $options) ? $options[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
4648
$processor->processFiles($formatter);
4749
}
4850
$processor->saveOutput();
@@ -62,51 +64,43 @@ protected static function resolvePath($path, $prefix)
6264
/**
6365
* @param array $config
6466
*
65-
* @return bool
6667
* @throws \InvalidArgumentException
6768
*/
6869
protected static function validateConfiguration(array $config)
6970
{
70-
if (empty($config[static::CONFIG_MAIN_KEY])) {
71+
if (!array_key_exists(static::CONFIG_MAIN_KEY, $config)) {
7172
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
7273
}
7374

7475
if (!is_array($config[static::CONFIG_MAIN_KEY])) {
75-
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
76+
throw new \InvalidArgumentException('the extra.' . static::CONFIG_MAIN_KEY . ' setting must be an array of objects');
7677
}
7778

78-
foreach ($config[static::CONFIG_MAIN_KEY] as $options) {
79+
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $options) {
7980
if (!is_array($options)) {
80-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
81+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index] should be an array");
8182
}
8283

83-
static::validateMandatoryOptions($options);
84+
static::validateMandatoryOptions($options, $index);
8485
}
8586
}
8687

8788
/**
8889
* @param array $options
90+
* @param int $index
8991
*
9092
* @throws \InvalidArgumentException
9193
*/
92-
protected static function validateMandatoryOptions(array $options)
94+
protected static function validateMandatoryOptions(array $options, $index)
9395
{
94-
foreach (static::$mandatoryOptions as $optionIndex) {
95-
if (!isset($options[$optionIndex])) {
96-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} is required!");
96+
foreach (static::$mandatoryOptions as $optionIndex => $type) {
97+
if (!array_key_exists($optionIndex, $options)) {
98+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} is required!");
9799
}
98100

99-
switch ($optionIndex) {
100-
case static::OPTION_KEY_INPUT:
101-
if (!is_array($options[$optionIndex])) {
102-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should be array!");
103-
}
104-
break;
105-
case static::OPTION_KEY_OUTPUT:
106-
if (!is_string($options[$optionIndex])) {
107-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should string!");
108-
}
109-
break;
101+
$callable = "is_{$type}";
102+
if (!$callable($options[$optionIndex])) {
103+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} should be {$type}!");
110104
}
111105
}
112106
}

tests/phpunit/ScriptHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function validateOptionsOnValid()
164164
*/
165165
private function validateMandatoryOptions($config)
166166
{
167-
return $this->invokeMethod(new ScriptHandler(), 'validateMandatoryOptions', [$config]);
167+
return $this->invokeMethod(new ScriptHandler(), 'validateMandatoryOptions', [$config, 1]);
168168
}
169169

170170
/*** *************************** INTEGRATION *************************** ***/

0 commit comments

Comments
 (0)