Passed
Push — master ( ab4527...297d0e )
by Joschi
01:46
created

parseSourceSizeCalculatedValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 21
ccs 0
cts 12
cp 0
crap 12
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
49
{
50
51
    public static function createFromSourceSizeStr(string $sourceSizeStr, int $emPixel = 16)
52
    {
53
        $sourceSizeValue = self::parseSourceSizeValue($sourceSizeStr, $emPixel);
54
        echo $sourceSizeValue.' / '.$sourceSizeStr.PHP_EOL;
55
56
        return 'bla';
57
    }
58
59
    /**
60
     * Parse the length component
61
     *
62
     * @param string $sourceSizeStr Source size string
63
     * @param int $emPixel          EM to pixel ratio
64
     *
65
     * @return UnitLengthInterface Length component
66
     * @throws InvalidArgumentException If the source size string is ill-formatted
67
     */
68
    protected static function parseSourceSizeValue(string &$sourceSizeStr, int $emPixel = 16): UnitLengthInterface
69
    {
70
        // If the source size string ends with a parenthesis: Try to parse a calc() base length
71
        if (substr($sourceSizeStr, -1) === ')') {
72
            return self::parseSourceSizeCalculatedValue($sourceSizeStr, $emPixel);
73
        }
74
75
        // If the source size string is ill-formatted
76
        if (!preg_match('/^(.*\s+)?([^\s]+)$/', $sourceSizeStr, $sourceSizeStrMatch)) {
77
            throw new InvalidArgumentException(
78
                sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
79
                InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
80
            );
81
        }
82
83
        // Post-process the remaining string
84
        $sourceSizeStr = trim($sourceSizeStrMatch[1]);
85
86
        // Return the parsed length
87
        return LengthFactory::createLengthFromString($sourceSizeStrMatch[2], $emPixel);
88
    }
89
90
    /**
91
     * Parse a calc() based length value
92
     *
93
     * @param string $sourceSizeStr Source size string
94
     * @param int $emPixel          EM to pixel ratio
95
     *
96
     * @return UnitLengthInterface Length component
97
     * @throws InvalidArgumentException If the source size string is ill-formatted
98
     */
99
    protected static function parseSourceSizeCalculatedValue(
100
        string &$sourceSizeStr,
101
        int $emPixel = 16
102
    ): UnitLengthInterface {
103
        // Reverse-consume the source size string
104
        $sourceSizeRev = strrev($sourceSizeStr);
105
        $balance       = null;
106
        for ($pos = 0; $pos < strlen($sourceSizeStr); ++$pos) {
107
            $balance += self::getCharacterBalance($sourceSizeRev[$pos]);
108
            if ($balance === 0) {
109
                $length        = CalcLengthFactory::createFromString(substr($sourceSizeStr, -($pos + 5)),$emPixel);
0 ignored issues
show
Unused Code introduced by
The assignment to $length is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $length is correct as Jkphl\Respimgcss\Applica..., -$pos + 5), $emPixel) targeting Jkphl\Respimgcss\Applica...ory::createFromString() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
                $sourceSizeStr = trim(substr($sourceSizeStr, 0, -($pos + 5)));
111
112
                return LengthFactory::createLengthFromString('100vw');
113
            }
114
        }
115
116
        // Else: The source size string is ill-formatted
117
        throw new InvalidArgumentException(
118
            sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
119
            InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
120
        );
121
    }
122
123
    /**
124
     * Return the balance value for a particular value
125
     *
126
     * @param string $char Character
127
     *
128
     * @return int Balance value
129
     */
130
    protected static function getCharacterBalance($char): string
131
    {
132
        return ($char === ')') ? 1 : (($char === '(') ? -1 : 0);
133
    }
134
}