Lines 100.00% 48 / 48
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 addHandleMethod 100.00% 8 / 8 100.00% 1 / 1 3
 addDomainActionMethod 100.00% 11 / 11 100.00% 1 / 1 5
 declareMethod 100.00% 5 / 5 100.00% 1 / 1 3
 addFieldParameters 100.00% 4 / 4 100.00% 1 / 1 3
 buildDocblock 100.00% 13 / 13 100.00% 1 / 1 9
 buildPayload 100.00% 4 / 4 100.00% 1 / 1 2
 listReservedMemberNames 100.00% 3 / 3 100.00% 1 / 1 4
16final readonly class ActionMethodEmitter implements ReservesMemberNames
17{
18    private const string TARGET_PARAMETER = 'target';
19
20    private const array RESERVED_PARAMETER_NAMES = ['this'];
21
22    private const array RESERVED_TARGETED_PARAMETER_NAMES = [...self::RESERVED_PARAMETER_NAMES, self::TARGET_PARAMETER];
23
24    public function addHandleMethod(ClassType $class, ActionModel $action): void
25    {
26        $method = $this->declareMethod($class, $action);
27        $this->addFieldParameters($method, $action);
28
29        $method->setBody(\sprintf(
30            'return $this->entity->%s(%s%s);',
31            $action->definition->returnsResponse ? 'callActionForResponse' : 'callAction',
32            var_export($action->definition->name, true),
33            $action->fields === [] ? '' : ', ' . $this->buildPayload($action),
34        ));
35    }
36
37    public function addDomainActionMethod(ClassType $class, ActionModel $action): void
38    {
39        $method = $this->declareMethod($class, $action);
40
41        if ($action->definition->target->isTargetable) {
42            $method->addParameter(self::TARGET_PARAMETER)->setType(ActionTargetSource::class);
43        }
44
45        $this->addFieldParameters($method, $action);
46
47        $method->setBody(\sprintf(
48            'return $this->ha->%s(%s, %s%s);',
49            $action->definition->returnsResponse ? 'callActionForResponse' : 'callAction',
50            var_export($action->definition->domain . '.' . $action->definition->name, true),
51            $action->fields === [] ? '[]' : $this->buildPayload($action),
52            $action->definition->target->isTargetable ? ', $' . self::TARGET_PARAMETER : '',
53        ));
54    }
55
56    private function declareMethod(ClassType $class, ActionModel $action): Method
57    {
58        $method = $class->addMethod($action->method)
59            ->setReturnType($action->definition->returnsResponse ? ActionResponse::class : EventContext::class);
60
61        foreach ($this->buildDocblock($action) as $line) {
62            $method->addComment($line);
63        }
64
65        return $method;
66    }
67
68    private function addFieldParameters(Method $method, ActionModel $action): void
69    {
70        foreach ($action->fields as $field) {
71            $parameter = $method->addParameter($field->parameter)->setType($field->type()->native);
72
73            if (!$field->field->required) {
74                $parameter->setDefaultValue(null);
75            }
76        }
77    }
78
79    /** @return list<string> */
80    private function buildDocblock(ActionModel $action): array
81    {
82        $title = $action->definition->title;
83        $description = $action->definition->description;
84        $lines = $title === null ? [] : [$title];
85
86        if ($description !== null) {
87            $lines = [...$lines, ...($lines === [] ? [] : ['']), $description];
88        }
89
90        $params = [];
91
92        foreach ($action->fields as $field) {
93            $summary = $field->summary();
94
95            if (!$field->type()->needsDocblock() && $summary === '') {
96                continue;
97            }
98
99            $params[] = rtrim(\sprintf('@param %s $%s %s', $field->type()->docblock, $field->parameter, $summary));
100        }
101
102        if ($lines !== [] && $params !== []) {
103            $lines[] = '';
104        }
105
106        return [...$lines, ...$params];
107    }
108
109    private function buildPayload(ActionModel $action): string
110    {
111        $entries = '';
112
113        foreach ($action->fields as $field) {
114            $entries .= \sprintf("    %s => $%s,\n", var_export($field->field->name, true), $field->parameter);
115        }
116
117        return \sprintf("[\n%s]", $entries);
118    }
119
120    public function listReservedMemberNames(MemberScope $scope): array
121    {
122        return match ($scope) {
123            MemberScope::Parameters => self::RESERVED_PARAMETER_NAMES,
124            MemberScope::TargetedParameters => self::RESERVED_TARGETED_PARAMETER_NAMES,
125            default => [],
126        };
127    }
128}