Completed
Push — master ( f0e18d...81dcb1 )
by Joschi
02:31
created

CalcLengthFactory::refineCalculationTokens()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application\Factory
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Respimgcss\Application\Factory;
38
39
use ChrisKonnertz\StringCalc\Parser\Nodes\ContainerNode;
40
use ChrisKonnertz\StringCalc\Tokenizer\Token;
41
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
42
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
43
use Jkphl\Respimgcss\Application\Model\AbsoluteLength;
44
use Jkphl\Respimgcss\Application\Model\StringCalculator;
45
use Jkphl\Respimgcss\Application\Model\ViewportLength;
46
47
/**
48
 * AbstractLength factory for calc() based values
49
 *
50
 * @package    Jkphl\Respimgcss
51
 * @subpackage Jkphl\Respimgcss\Application\Factory
52
 */
53
class CalcLengthFactory extends AbstractLengthFactory
54
{
55
    /**
56
     * Create a unit length from a calc() size string
57
     *
58
     * @param string $calcString calc() size string
59
     *
60
     * @return UnitLengthInterface
61
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
62
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
63
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
64
     */
65 1
    public function createFromString(string $calcString): UnitLengthInterface
66
    {
67
        // If the calc() string is ill-formatted
68 1
        if (!preg_match('/^calc\(.+\)$/', $calcString)) {
69
            throw new InvalidArgumentException(
70
                sprintf(InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING_STR, $calcString),
71
                InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING
72
            );
73
        }
74
75 1
        return $this->createCalculationContainerFromString($calcString);
76
    }
77
78
    /**
79
     * Parse a calculation string and return a precompiled calculation node container
80
     *
81
     * @param string $calcString Calculation string
82
     *
83
     * @return ContainerNode Calculation node container
84
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
85
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
86
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
87
     */
88 1
    protected function createCalculationContainerFromString(string $calcString): UnitLengthInterface
89
    {
90 1
        $stringCalc    = new StringCalculator();
91 1
        $calcTokens    = $stringCalc->tokenize($calcString);
92 1
        $refinedTokens = $this->refineCalculationTokens($calcTokens);
93
94
        // If there's the viewport involved in the calculation: Create a relative calculated length
95
        /** @var Token $token */
96 1
        foreach ($refinedTokens as $token) {
97 1
            if (($token->getType() == Token::TYPE_WORD) && ($token->getValue() === 'viewport')) {
98 1
                return new ViewportLength(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Jkphl\Respimg...erService, $calcString) returns the type Jkphl\Respimgcss\Application\Model\ViewportLength which is incompatible with the documented return type ChrisKonnertz\StringCalc...ser\Nodes\ContainerNode.
Loading history...
99 1
                    $stringCalc->parse($refinedTokens),
100 1
                    $this->lengthNormalizerService,
101 1
                    $calcString
102
                );
103
            }
104
        }
105
106
        // Create and return an absolute length
107
        return new AbsoluteLength(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Jkphl\Respimg...engthNormalizerService) returns the type Jkphl\Respimgcss\Application\Model\AbsoluteLength which is incompatible with the documented return type ChrisKonnertz\StringCalc...ser\Nodes\ContainerNode.
Loading history...
108
            $stringCalc->calculate($refinedTokens),
0 ignored issues
show
Bug introduced by
$refinedTokens of type array is incompatible with the type string expected by parameter $term of ChrisKonnertz\StringCalc\StringCalc::calculate(). ( Ignorable by Annotation )

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

108
            $stringCalc->calculate(/** @scrutinizer ignore-type */ $refinedTokens),
Loading history...
109
            UnitLengthInterface::UNIT_PIXEL,
110
            $this->lengthNormalizerService
111
        );
112
    }
113
114
    /**
115
     * Refine a list of symbol tokens
116
     *
117
     * @param Token[] $tokens Symbol tokens
118
     *
119
     * @return Token[] Refined symbol tokens
120
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
121
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
122
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
123
     */
124 1
    protected function refineCalculationTokens(array $tokens): array
125
    {
126 1
        $refinedTokens = [];
127 1
        $previousToken = null;
128
129
        // Run through all tokens
130 1
        foreach ($tokens as $token) {
131 1
            $previousToken = $this->handleToken($refinedTokens, $token, $previousToken);
132
        }
133
134
        // Add the last token
135 1
        if ($previousToken) {
136
            array_push($refinedTokens, $previousToken);
137
        }
138
139 1
        return $refinedTokens;
140
    }
141
142
    /**
143
     * Handle a particular token
144
     *
145
     * @param Token[] $refinedTokens    Refined tokens
146
     * @param Token $token              Token
147
     * @param Token|null $previousToken Previous token
148
     *
149
     * @return Token|null               Stash token
150
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
151
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
152
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
153
     */
154 1
    protected function handleToken(array &$refinedTokens, Token $token, Token $previousToken = null): ?Token
155
    {
156
        // If it's a word token: Handle individually
157 1
        if ($token->getType() == Token::TYPE_WORD) {
158 1
            return $this->handleWordToken($refinedTokens, $token, $previousToken);
159
        }
160
161
        // In all other cases: Register the previou token (if any)
162 1
        if ($previousToken) {
163
            array_push($refinedTokens, $previousToken);
164
        }
165
166
        // If it's a number token: Stash
167 1
        if ($token->getType() == Token::TYPE_NUMBER) {
168 1
            return $token;
169
        }
170
171 1
        array_push($refinedTokens, $token);
172
173 1
        return null;
174
    }
175
176
    /**
177
     * Handle a particular token
178
     *
179
     * The method returns a list of zero or more (possibly refined) tokens to preerve
180
     *
181
     * @param Token[] $refinedTokens    Refined tokens
182
     * @param Token $token              Token
183
     * @param Token|null $previousToken Previous token
184
     *
185
     * @return Token|null               Stash token
186
     * @throws InvalidArgumentException If the word token is invalid
187
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
188
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
189
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
190
     */
191 1
    protected function handleWordToken(array &$refinedTokens, Token $token, Token $previousToken = null): ?Token
192
    {
193
        // If it's a calc() function call: Add the previous token and skip the current one
194 1
        if ($token->getValue() == 'calc') {
195 1
            if ($previousToken) {
196
                array_push($refinedTokens, $previousToken);
197
            }
198
199 1
            return null;
200
        }
201
202
        // If the previous token is a number: Try to generate a unit length
203 1
        if ($previousToken && ($previousToken->getType() == Token::TYPE_NUMBER)) {
204
            try {
205 1
                $unitLength = (new LengthFactory($this->emPixel))->createLengthFromString(
206 1
                    $previousToken->getValue().$token->getValue()
207
                );
208 1
                $this->handleUnitLengthToken($refinedTokens, $unitLength);
209
210 1
                return null;
211
            } catch (InvalidArgumentException $e) {
212
                // Ignore
213
            }
214
        }
215
216
        // Invalid word token
217
        throw new InvalidArgumentException(
218
            sprintf(InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE_STR, $token->getValue()),
219
            InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE
220
        );
221
    }
222
223
    /**
224
     * Handle a unit length token
225
     *
226
     * @param Token[] $refinedTokens          Refined tokens
227
     * @param UnitLengthInterface $unitLength Unit length
228
     */
229 1
    protected function handleUnitLengthToken(
230
        array &$refinedTokens,
231
        UnitLengthInterface $unitLength
232
    ): void {
233
        // If it's an absolute value
234 1
        if ($unitLength->isAbsolute()) {
235 1
            array_push($refinedTokens, new Token(strval($unitLength->getValue()), Token::TYPE_NUMBER, 0));
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Jkphl\Respimgcss\Applica...act\UnitLengthInterface. Did you maybe mean getValueAndUnit()? ( Ignorable by Annotation )

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

235
            array_push($refinedTokens, new Token(strval($unitLength->/** @scrutinizer ignore-call */ getValue()), Token::TYPE_NUMBER, 0));

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...
236
237 1
            return;
238
        }
239
240
        // Else: Substitute with multiplied function expression
241 1
        array_push(
242 1
            $refinedTokens,
243 1
            new Token('(', Token::TYPE_CHARACTER, 0),
244 1
            new Token(strval($unitLength->getOriginalValue() / 100), Token::TYPE_NUMBER, 0),
245 1
            new Token('*', Token::TYPE_CHARACTER, 0),
246 1
            new Token('viewport', Token::TYPE_WORD, 0),
247 1
            new Token('(', Token::TYPE_CHARACTER, 0),
248 1
            new Token(')', Token::TYPE_CHARACTER, 0),
249 1
            new Token(')', Token::TYPE_CHARACTER, 0)
250
        );
251
    }
252
}