MethodSetProcessor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 70
ccs 32
cts 32
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createCollectionSetterBody() 0 27 1
A process() 0 4 2
A __construct() 0 2 1
A createDefaultSetterBody() 0 3 1
A provideMethod() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Preparation\Processor;
15
16
use Micro\Library\DTO\ClassDef\ClassDefinition;
17
use Micro\Library\DTO\ClassDef\MethodDefinition;
18
use Micro\Library\DTO\ClassDef\PropertyDefinition;
19
use Micro\Library\DTO\Helper\NameNormalizerInterface;
20
use Micro\Library\DTO\Preparation\PreparationProcessorInterface;
21
22
class MethodSetProcessor implements PreparationProcessorInterface
23
{
24
    /**
25
     * @param NameNormalizerInterface $nameNormalizer
26
     */
27 6
    public function __construct(private NameNormalizerInterface $nameNormalizer)
28
    {
29 6
    }
30
31 1
    public function process(array $classDef, ClassDefinition $classDefinition, array $classList): void
32
    {
33 1
        foreach ($classDefinition->getProperties() as $property) {
34 1
            $this->provideMethod($classDefinition, $property);
35
        }
36
    }
37
38 1
    protected function provideMethod(ClassDefinition $classDefinition, PropertyDefinition $propertyDefinition): void
39
    {
40 1
        $propertyName = $propertyDefinition->getName();
41 1
        $methodSuffix = $this->nameNormalizer->normalize($propertyName);
42
43 1
        $methodDef = new MethodDefinition();
44 1
        $methodDef->setName('set'.ucfirst($methodSuffix));
45
        // $methodDef->setBody(sprintf("\$this->%s = $%s;\r\n\r\nreturn \$this;", $propertyName, $propertyName));
46
47 1
        if ($propertyDefinition->isCollection()) {
48 1
            $this->createCollectionSetterBody($methodDef, $propertyName);
49
        } else {
50 1
            $this->createDefaultSetterBody($methodDef, $propertyName);
51
        }
52
53 1
        $methodDef->setArgs([$propertyDefinition]);
54 1
        $methodDef->setTypesReturn(['self']);
55 1
        $classDefinition->addMethod($methodDef);
56
    }
57
58 1
    protected function createCollectionSetterBody(MethodDefinition $methodDefinition, string $propertyName): void
59
    {
60 1
        $methodDefinition->setBody(
61 1
            sprintf(
62 1
                '
63
            if(!$%s) {
64
                $this->%s = null;
65
66
                return $this;
67
            }
68
69
            if(!$this->%s) {
70
                $this->%s = new %s();
71
            }
72
73
            foreach($%s as $item) {
74
                $this->%s->add($item);
75
            }
76
77 1
            return $this;',
78 1
                $propertyName,
79 1
                $propertyName,
80 1
                $propertyName,
81 1
                $propertyName,
82 1
                'Collection',
83 1
                $propertyName,
84 1
                $propertyName
85 1
            )
86 1
        );
87
    }
88
89 1
    protected function createDefaultSetterBody(MethodDefinition $methodDefinition, string $propertyName): void
90
    {
91 1
        $methodDefinition->setBody(sprintf("\$this->%s = $%s;\r\n\r\nreturn \$this;", $propertyName, $propertyName));
92
    }
93
}
94