Passed
Push — master ( 1ada49...6f88dc )
by Bjørn
02:29
created

OptionsTrait::checkOptions()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 13
nop 0
dl 0
loc 34
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\InvalidOptionTypeException;
6
7
trait OptionsTrait
8
{
9
10
    /** @var array  Provided conversion options */
11
    public $providedOptions;
12
13
    /** @var array  Calculated conversion options (merge of default options and provided options)*/
14
    public $options;
15
16
17
    public static $optionDefinitionsBasic = [
18
        ['quality', 'string|number', 'auto'],
19
        ['max-quality', 'number', 85],
20
        ['default-quality', 'number', 75],
21
        ['metadata', 'string', 'none'],
22
        ['method', 'number', 6],
23
        ['low-memory', 'boolean', false],
24
        ['lossless', 'boolean', false],
25
        ['skip-pngs', 'boolean', false],
26
    ];
27
28
    /**
29
     * Set logger
30
     *
31
     * @param   array $options (optional)
32
     * @return  void
33
     */
34
    public function setProvidedOptions($providedOptions = [])
35
    {
36
        $this->providedOptions = $providedOptions;
37
38
        // -  Merge $defaultOptions into provided options
39
        $this->options = array_merge($this->getDefaultOptions(), $this->providedOptions);
40
    }
41
42
43
    public function getAllOptionDefinitions()
44
    {
45
        return array_merge(self::$optionDefinitionsBasic, $this->getOptionDefinitionsExtra());
0 ignored issues
show
Bug introduced by
The method getOptionDefinitionsExtra() does not exist on WebPConvert\Convert\Base...BaseTraits\OptionsTrait. Did you maybe mean getAllOptionDefinitions()? ( Ignorable by Annotation )

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

45
        return array_merge(self::$optionDefinitionsBasic, $this->/** @scrutinizer ignore-call */ getOptionDefinitionsExtra());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
    }
47
48
    public function getDefaultOptions()
49
    {
50
        $defaults = [];
51
        foreach ($this->getAllOptionDefinitions() as list($name, $type, $default)) {
52
            $defaults[$name] = $default;
53
        }
54
        return $defaults;
55
    }
56
57
    protected function checkOptions()
58
    {
59
        foreach ($this->getAllOptionDefinitions() as $def) {
60
            list($optionName, $optionType) = $def;
61
62
            if (isset($this->providedOptions[$optionName])) {
63
                //$this->logLn($optionName);
64
65
                $actualType = gettype($this->providedOptions[$optionName]);
66
                if ($actualType != $optionType) {
67
                    $optionType = str_replace('number', 'integer|double', $optionType);
68
                    if (!in_array($actualType, explode('|', $optionType))) {
69
                        throw new InvalidOptionTypeException(
70
                            'The provided ' . $optionName . ' option is not a ' . $optionType .
71
                                ' (it is a ' . $actualType . ')'
72
                        );
73
                    }
74
                }
75
76
                $optionValue = $this->providedOptions[$optionName];
77
78
                if ($optionName == 'quality') {
79
                    if ($actualType == 'string') {
80
                        if ($optionValue != 'auto') {
81
                            throw new InvalidOptionTypeException(
82
                                'Quality must be eithe "auto" or a number between 0-100. ' .
83
                                'A string, "' . $optionValue . '" was given'
84
                            );
85
                        }
86
                    } else {
87
                        if (($optionValue < 0) | ($optionValue > 100)) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
88
                            throw new InvalidOptionTypeException(
89
                                'Quality must be eithe "auto" or a number between 0-100. ' .
90
                                    'The number you provided (' . strval($optionValue) . ') is out of range.'
91
                            );
92
                        }
93
                    }
94
                }
95
            }
96
        }
97
    }
98
99
    /**
100
     * Prepare options.
101
     */
102
     /*
103
    private function prepareOptions()
104
    {
105
        //$defaultOptions = self::$defaultOptions;
106
107
        // -  Merge defaults of the converters extra options into the standard default options.
108
        //$defaultOptions = array_merge($defaultOptions, array_column(static::$extraOptions, 'default', 'name'));
109
        //print_r($this->getOptionDefinitionsExtra());
110
        //$extra = [];
111
        //$this->getDefaultOptionsExtra();
112
        //echo '<br>';
113
        //print_r(static::$extraOptions);
114
        //print_r(array_column(static::$extraOptions, 'default', 'name'));
115
        //$defaultOptions = array_merge($defaultOptions, $this->getDefaultOptionsExtra());
116
117
118
        //throw new \Exception('extra!' . print_r($this->getConverterDisplayName(), true));
119
120
        // -  Merge $defaultOptions into provided options
121
        //$this->options = array_merge($defaultOptions, $this->options);
122
        //$this->options = array_merge($this->getDefaultOptions(), $providedOptions);
123
124
        if ($this->getMimeTypeOfSource() == 'png') {
125
            // skip png's ?
126
            if ($this->options['skip-pngs']) {
127
                throw new ConversionDeclinedException(
128
                    'PNG file skipped (configured to do so)'
129
                );
130
            }
131
132
            // Force lossless option to true for PNG images
133
            $this->options['lossless'] = true;
134
        }
135
136
137
        // TODO: Here we could test if quality is 0-100 or auto.
138
        //       and if not, throw something extending InvalidArgumentException (which is a LogicException)
139
    }*/
140
}
141