GenerateSequenceCompanion   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 34 3
A render() 0 23 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Generator\Command;
4
5
use ICanBoogie\CLDR\Repository;
6
use Symfony\Component\Console\Attribute\AsCommand;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
#[AsCommand(self::GENERATED_FILE)]
12
final class GenerateSequenceCompanion extends Command
13
{
14
    private const GENERATED_FILE = 'src/Supplemental/Units/SequenceCompanion.php';
15
16
    public function __construct(
17
        private readonly Repository $repository
18
    ) {
19
        parent::__construct();
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output): int
23
    {
24
        $units = $this->repository->locale_for('en-001')['units']['long'];
25
        $methods = [];
26
27
        foreach ($units as $name => $unit) {
28
            if (empty($unit['unitPattern-count-one'])) {
29
                continue;
30
            }
31
32
            $normalized = strtr($name, [ '-' => '_' ]);
33
34
            $methods[] = <<<PHP
35
                /**
36
                 * @param float|int|numeric-string \$number
37
                 *
38
                 * @return \$this
39
                 */
40
                public function $normalized(float|int|string \$number): self
41
                {
42
                    \$this->sequence["$name"] = \$number;
43
44
                    return \$this;
45
                }
46
            PHP;
47
        }
48
49
        $contents = $this->render(
50
            methods: implode("\n\n", $methods),
51
        );
52
53
        file_put_contents(self::GENERATED_FILE, $contents);
54
55
        return self::SUCCESS;
56
    }
57
58
    private function render(
59
        string $methods,
60
    ): string {
61
        $class = __CLASS__;
62
63
        return <<<PHP
64
        <?php
65
66
        /**
67
         * CODE GENERATED; DO NOT EDIT.
68
         *
69
         * {@see \\$class}
70
         */
71
72
        namespace ICanBoogie\CLDR\Supplemental\Units;
73
74
        /**
75
         * @internal
76
         * @codeCoverageIgnore
77
         */
78
        trait SequenceCompanion
79
        {
80
        $methods
81
        }
82
83
        PHP;
84
    }
85
}
86