Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

AttributeTrait::sanitize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\File\Attributes;
13
14
use Liip\ImagineBundle\Exception\InvalidArgumentException;
15
16
/**
17
 * @internal
18
 *
19
 * @author Rob Frawley 2nd <[email protected]>
20
 */
21
trait AttributeTrait
22
{
23
    /**
24
     * @return string
25
     */
26
    public function __toString(): string
27
    {
28
        return $this->stringify();
29
    }
30
31
    /**
32
     * @param string|null $string
33
     *
34
     * @return self
35
     */
36
    public static function create(string $string = null): self
37
    {
38
        if (null !== $sections = self::explodeParsable($string)) {
39
            return new self(...array_values($sections));
0 ignored issues
show
Unused Code introduced by
The call to AttributeTrait::__construct() has too many arguments starting with array_values($sections).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
        }
41
42
        return new self();
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    abstract public function stringify(): string;
49
50
    /**
51
     * @param string|null $string
52
     *
53
     * @return bool
54
     */
55
    public static function isParsable(string $string = null): bool
56
    {
57
        try {
58
            return true !== empty($string)
59
                && null !== self::explodeParsable($string);
60
        } catch (InvalidArgumentException $exception) {
61
            return false;
62
        }
63
    }
64
65
    /**
66
     * @param string $string
67
     *
68
     * @return array|null
69
     */
70
    abstract public static function explodeParsable(string $string = null): ?array;
71
72
    /**
73
     * @param string|null $string
74
     *
75
     * @return null|string
76
     */
77
    private static function sanitize(string $string = null): ?string
78
    {
79
        if (null === $string || 1 !== preg_match('{(?<characters>[^a-z0-9\.-]+)}i', $string, $matches)) {
80
            return $string;
81
        }
82
83
        throw new InvalidArgumentException(
84
            'Invalid character(s) "%s" provided in "%s" (accepted values are "[a-z0-9\.-]").',
85
            $matches['characters'], $string
86
        );
87
    }
88
}
89