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

parseSourceSizeCalculatedValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 21
ccs 10
cts 13
cp 0.7692
crap 3.1105
rs 9.3142
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 Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
40
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
41
42
/**
43
 * Source size factory
44
 *
45
 * @package    Jkphl\Respimgcss
46
 * @subpackage Jkphl\Respimgcss\Application\Factory
47
 */
48
class SourceSizeFactory extends AbstractLengthFactory
49
{
50
    /**
51
     * Create a source size value from a source size string
52
     *
53
     * @param string $sourceSizeStr Source size string
54
     *
55
     * @return UnitLengthInterface Source size value
56
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
57
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
58
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
59
     */
60 1
    public function createFromSourceSizeStr(string $sourceSizeStr)
61
    {
62 1
        echo $sourceSizeStr.' -> ';
63 1
        $sourceSizeValue = $this->parseSourceSizeValue($sourceSizeStr);
64 1
        echo $sourceSizeValue->getValueAndUnit().' ('.get_class($sourceSizeValue).')'.PHP_EOL;
65
66 1
        return $sourceSizeValue;
67
    }
68
69
    /**
70
     * Parse the length component
71
     *
72
     * @param string $sourceSizeStr Source size string
73
     *
74
     * @return UnitLengthInterface AbstractLength component
75
     * @throws InvalidArgumentException If the source size string is ill-formatted
76
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
77
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
78
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
79
     */
80 1
    protected function parseSourceSizeValue(string &$sourceSizeStr): UnitLengthInterface
81
    {
82
        // If the source size string ends with a parenthesis: Try to parse a calc() base length
83 1
        if (substr($sourceSizeStr, -1) === ')') {
84 1
            return $this->parseSourceSizeCalculatedValue($sourceSizeStr);
85
        }
86
87
        // If the source size string is ill-formatted
88 1
        if (!preg_match('/^(.*\s+)?([^\s]+)$/', $sourceSizeStr, $sourceSizeStrMatch)) {
89
            throw new InvalidArgumentException(
90
                sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
91
                InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
92
            );
93
        }
94
95
        // Post-process the remaining string
96 1
        $sourceSizeStr = trim($sourceSizeStrMatch[1]);
97
98
        // Return the parsed length
99 1
        return (new LengthFactory($this->emPixel))->createLengthFromString($sourceSizeStrMatch[2], $this->emPixel);
0 ignored issues
show
Unused Code introduced by
The call to Jkphl\Respimgcss\Applica...reateLengthFromString() has too many arguments starting with $this->emPixel. ( Ignorable by Annotation )

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

99
        return (new LengthFactory($this->emPixel))->/** @scrutinizer ignore-call */ createLengthFromString($sourceSizeStrMatch[2], $this->emPixel);

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. Please note the @ignore annotation hint above.

Loading history...
100
    }
101
102
    /**
103
     * Parse a calc() based length value
104
     *
105
     * @param string $sourceSizeStr Source size string
106
     *
107
     * @return UnitLengthInterface AbstractLength component
108
     * @throws InvalidArgumentException If the source size string is ill-formatted
109
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
110
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
111
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
112
     */
113 1
    protected function parseSourceSizeCalculatedValue(string &$sourceSizeStr): UnitLengthInterface
114
    {
115
        // Reverse-consume the source size string
116 1
        $sourceSizeRev = strrev($sourceSizeStr);
117 1
        $balance       = null;
118 1
        for ($pos = 0; $pos < strlen($sourceSizeStr); ++$pos) {
119 1
            $balance += $this->getCharacterBalance($sourceSizeRev[$pos]);
120 1
            if ($balance === 0) {
121 1
                $length        = (new CalcLengthFactory($this->emPixel))->createFromString(
122 1
                    substr($sourceSizeStr, -($pos + 5))
123
                );
124 1
                $sourceSizeStr = trim(substr($sourceSizeStr, 0, -($pos + 5)));
125
126 1
                return $length;
127
            }
128
        }
129
130
        // Else: The source size string is ill-formatted
131
        throw new InvalidArgumentException(
132
            sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
133
            InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
134
        );
135
    }
136
137
    /**
138
     * Return the balance value for a particular value
139
     *
140
     * @param string $char Character
141
     *
142
     * @return int Balance value
143
     */
144 1
    protected function getCharacterBalance($char): string
145
    {
146 1
        return ($char === ')') ? 1 : (($char === '(') ? -1 : 0);
147
    }
148
}