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

CommonTrait::processWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
crap 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