Passed
Push — master ( bf6fa9...817f88 )
by Nikolaos
06:58
created

CommonTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 46
rs 10
c 0
b 0
f 0
ccs 0
cts 15
cp 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cloneInstance() 0 6 1
A processWith() 0 5 1
A checkStringParameter() 0 5 2
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Http\Message\Traits;
15
16
use Phalcon\Http\Message\Exception\InvalidArgumentException;
17
18
use function is_string;
19
20
/**
21
 * Trait Common
22
 */
23
trait CommonTrait
24
{
25
    /**
26
     * Returns a new instance having set the parameter
27
     *
28
     * @param mixed  $element
29
     * @param string $property
30
     *
31
     * @return mixed
32
     */
33
    protected function cloneInstance($element, string $property)
34
    {
35
        $newInstance              = clone $this;
36
        $newInstance->{$property} = $element;
37
38
        return $newInstance;
39
    }
40
41
    /**
42
     * Checks the element passed if it is a string
43
     *
44
     * @param mixed $element
45
     */
46
    private function checkStringParameter($element): void
47
    {
48
        if (!is_string($element)) {
49
            throw new InvalidArgumentException(
50
                'Method requires a string argument'
51
            );
52
        }
53
    }
54
55
    /**
56
     * Checks the element passed; assigns it to the property and returns a
57
     * clone of the object back
58
     *
59
     * @param mixed  $element
60
     * @param string $property
61
     *
62
     * @return mixed
63
     */
64
    private function processWith($element, string $property)
65
    {
66
        $this->checkStringParameter($element);
67
68
        return $this->cloneInstance($element, $property);
69
    }
70
}
71