Passed
Push — master ( 8719f0...ab4527 )
by Joschi
02:24
created

SourceSizeFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 19
cts 25
cp 0.76
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseSourceSizeCalculatedValue() 0 17 3
A createFromSourceSizeStr() 0 6 1
A parseSourceSizeValue() 0 20 3
A getCharacterBalance() 0 3 3
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 1
    public static function createFromSourceSizeStr(string $sourceSizeStr)
52
    {
53 1
        $sourceSizeValue = self::parseSourceSizeValue($sourceSizeStr);
54 1
        echo $sourceSizeValue.' / '.$sourceSizeStr.PHP_EOL;
55
56 1
        return 'bla';
57
    }
58
59
    /**
60
     * Parse the length component
61
     *
62
     * @param string $sourceSizeStr Source size string
63
     *
64
     * @return UnitLengthInterface Length component
65
     * @throws InvalidArgumentException If the source size string is ill-formatted
66
     */
67 1
    protected static function parseSourceSizeValue(string &$sourceSizeStr): UnitLengthInterface
68
    {
69
        // If the source size string ends with a parenthesis: Try to parse a calc() base length
70 1
        if (substr($sourceSizeStr, -1) === ')') {
71 1
            return self::parseSourceSizeCalculatedValue($sourceSizeStr);
72
        }
73
74
        // If the source size string is ill-formatted
75 1
        if (!preg_match('/^(.*\s+)?([^\s]+)$/', $sourceSizeStr, $sourceSizeStrMatch)) {
76
            throw new InvalidArgumentException(
77
                sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
78
                InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
79
            );
80
        }
81
82
        // Post-process the remaining string
83 1
        $sourceSizeStr = trim($sourceSizeStrMatch[1]);
84
85
        // Return the parsed length
86 1
        return LengthFactory::createLengthFromString($sourceSizeStrMatch[2]);
87
    }
88
89
    /**
90
     * Parse a calc() based length value
91
     *
92
     * @param string $sourceSizeStr Source size string
93
     *
94
     * @return UnitLengthInterface Length component
95
     * @throws InvalidArgumentException If the source size string is ill-formatted
96
     */
97 1
    protected static function parseSourceSizeCalculatedValue(string &$sourceSizeStr): UnitLengthInterface
98
    {
99
        // Reverse-consume the source size string
100 1
        for ($pos = 0, $sourceSizeRev = strrev($sourceSizeStr), $balance = null; $pos < strlen($sourceSizeStr); ++$pos) {
101 1
            $balance += self::getCharacterBalance($sourceSizeRev[$pos]);
102 1
            if ($balance === 0) {
103 1
                $length        = CalcLengthFactory::createFromString(substr($sourceSizeStr, -($pos + 5)));
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...rceSizeStr, -$pos + 5)) 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...
104 1
                $sourceSizeStr = trim(substr($sourceSizeStr, 0, -($pos + 5)));
105
106 1
                return LengthFactory::createLengthFromString('100vw');
107
            }
108
        }
109
110
        // Else: The source size string is ill-formatted
111
        throw new InvalidArgumentException(
112
            sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
113
            InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
114
        );
115
    }
116
117
    /**
118
     * Return the balance value for a particular value
119
     *
120
     * @param string $char Character
121
     *
122
     * @return int Balance value
123
     */
124 1
    protected static function getCharacterBalance($char): string
125
    {
126 1
        return ($char === ')') ? 1 : (($char === '(') ? -1 : 0);
127
    }
128
}