Completed
Push — master ( f18951...94bfb9 )
by Randy
03:34
created

Hydrate::new()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Object\ObjectFacade;
6
use Dgame\Soap\AssignableInterface;
7
use ICanBoogie\Inflector;
8
9
/**
10
 * Class Hydrate
11
 * @package Dgame\Soap\Hydrator
12
 */
13
final class Hydrate extends ObjectFacade
14
{
15
    /**
16
     * @param string $class
17
     *
18
     * @return Hydrate
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
19
     */
20 6
    public static function new(string $class): self
21
    {
22 6
        return new self(new $class());
23
    }
24
25
    /**
26
     * @param Hydrate $hydrat
0 ignored issues
show
Documentation introduced by
Should the type for parameter $hydrat not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
     *
28
     * @return bool
29
     */
30 4
    public function append(self $hydrat): bool
31
    {
32 4
        return $this->assignValue($hydrat->getClassName(), $hydrat->getObject());
33
    }
34
35
    /**
36
     * @param AssignableInterface $assignable
37
     *
38
     * @return bool
39
     */
40 6
    public function assign(AssignableInterface $assignable): bool
41
    {
42 6
        if ($assignable->hasValue()) {
43 6
            return $this->assignValue($assignable->getName(), $assignable->getValue());
44
        }
45
46 1
        return false;
47
    }
48
49
    /**
50
     * @param string $name
51
     * @param mixed  $value
52
     *
53
     * @return bool
54
     */
55 6
    public function assignValue(string $name, $value): bool
56
    {
57
        $names = [
58 6
            Inflector::get()->camelize($name, Inflector::DOWNCASE_FIRST_LETTER),
59 6
            Inflector::get()->camelize($name, Inflector::UPCASE_FIRST_LETTER)
60
        ];
61
62 6
        foreach ($names as $name) {
63 6
            if ($this->setValueByMethod($name, $value) || $this->setValueByProperty($name, $value)) {
64 6
                return true;
65
            }
66
        }
67
68 1
        return false;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 4
    public function getClassName(): string
75
    {
76 4
        return $this->getReflection()->getShortName();
77
    }
78
}