Passed
Push — feature/issue-124 ( 167236...6b1eff )
by Mikaël
07:01
created

ClassMap::addMethodBody()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
9
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
10
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
11
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
12
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
13
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
14
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
15
16
final class ClassMap extends AbstractModelFile
17
{
18
    public const METHOD_NAME = 'get';
19
20 23
    protected function fillClassConstants(ConstantContainer $constants): void
21
    {
22 23
    }
23
24
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
25
    {
26
    }
27
28 23
    protected function fillClassProperties(PropertyContainer $properties): void
29
    {
30 23
    }
31
32
    protected function getPropertyAnnotationBlock(PhpProperty $property): ?PhpAnnotationBlock
33
    {
34
    }
35
36 23
    protected function fillClassMethods(): void
37
    {
38 23
        $method = new PhpMethod(self::METHOD_NAME, [], self::TYPE_ARRAY, PhpMethod::ACCESS_PUBLIC, false, true, true);
39 23
        $this->addMethodBody($method);
40 23
        $this->methods->add($method);
41 23
    }
42
43 23
    protected function getMethodAnnotationBlock(PhpMethod $method): ?PhpAnnotationBlock
44
    {
45 23
        return new PhpAnnotationBlock([
46 23
            'Returns the mapping between the WSDL Structs and generated Structs\' classes',
47 23
            'This array is sent to the \SoapClient when calling the WS',
48 23
            new PhpAnnotation(AbstractModelFile::ANNOTATION_RETURN, 'string[]'),
49
        ]);
50
    }
51
52 23
    protected function getClassAnnotationBlock(): PhpAnnotationBlock
53
    {
54 12
        $annotations = [
55 11
            'Class which returns the class map definition',
56
        ];
57
58 23
        if (!empty($this->getGenerator()->getOptionPrefix())) {
59 7
            $annotations[] = new PhpAnnotation(self::ANNOTATION_PACKAGE, $this->getGenerator()->getOptionPrefix());
60
        }
61
62 23
        return new PhpAnnotationBlock($annotations);
63
    }
64
65 23
    protected function addMethodBody(PhpMethod $method): self
66
    {
67 23
        if ($this->getGenerator()->getStructs()->count() > 0) {
68 23
            $method->addChild('return [');
69 23
            foreach ($this->getGenerator()->getStructs() as $struct) {
70 23
                $this->addStructToClassMapList($method, $struct);
71
            }
72 23
            $method->addChild('];');
73
        }
74
75 23
        return $this;
76
    }
77
78 23
    protected function addStructToClassMapList(PhpMethod $method, StructModel $struct): self
79
    {
80 23
        if ($struct->isStruct() && !$struct->isRestriction()) {
81 23
            $method->addChild($method->getIndentedString(sprintf('\'%s\' => \'%s\',', $struct->getName(), $this->getStructName($struct)), 1));
82
        }
83
84 23
        return $this;
85
    }
86
87
    /**
88
     * Work around for https://bugs.php.net/bug.php?id=69280.
89
     */
90 23
    protected function getStructName(StructModel $struct): string
91
    {
92 23
        return str_replace('\\', '\\\\', $struct->getPackagedName(true));
93
    }
94
}
95