aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/SORM/DescribeModels.php
blob: 43ce21f706b8b01b89ee3359d152e2b560b8c601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
namespace Studip\Cli\Commands\SORM;

use Error;
use Exception;
use I18NString;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use ReflectionClass;
use SimpleORMap;
use SimpleORMapCollection;
use Studip\Cli\Commands\AbstractCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class DescribeModels extends AbstractCommand
{
    protected static $defaultName = 'sorm:describe';

    private ProgressBar $progress;
    private ReflectionClass $reflection;

    protected function configure(): void
    {
        $this->setDescription('Describe models');
        $this->setHelp('This command will add neccessary @property annotations to SimpleORMap classes in a folder');

        $this->addArgument(
            'folder',
            InputArgument::OPTIONAL,
            'Folder to scan (will default to lib/models)',
            'lib/models'
        );

        $this->addOption(
            'recursive',
            'r',
            InputOption::VALUE_NEGATABLE,
            'Scan into subfolders recursively'
        );
        $this->addOption(
            'bootstrap',
            'b',
            InputOption::VALUE_OPTIONAL,
            'Execute bootstrap file before scanning folder'
        );
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $bootstrap = $input->getOption('bootstrap');
        if ($bootstrap) {
            if (!file_exists($bootstrap)) {
                throw new Exception("Invalid bootstrap file {$bootstrap} provided");
            }
            require_once $bootstrap;
        }

        $recursive = $input->getOption('recursive') ?? false;
        $folder = $input->getArgument('folder');
        $iterator = $this->getFolderIterator($folder, $recursive, ['php']);

        $this->progress = new ProgressBar($output, iterator_count($iterator));
        $this->progress->start();

        foreach ($iterator as $file) {
            $filename = $file->getFilename();

            if (!is_writable($file->getRealPath())) {
                $this->outputForFile(
                    $output,
                    '<comment>Skipping not writable file ' . $this->relativeFilePath($file->getPathname()) . '</comment>'
                );
                continue;
            }

            $class_name = str_replace('.class.php', '.php', $filename);
            $class_name = pathinfo($class_name, PATHINFO_FILENAME);
            if (!class_exists($class_name)) {
                $class_name = $this->getClassNameFromFile($file->getPathname()) ?? $class_name;
            }

            if (!class_exists($class_name) || !is_subclass_of($class_name, SimpleORMap::class)) {
                $this->outputForFile(
                    $output,
                    "Skipping invalid class file {$filename} (class {$class_name})",
                    OutputInterface::VERBOSITY_VERBOSE
                );
                continue;
            }

            try {
                $this->reflection = new ReflectionClass($class_name);
            } catch (Error $e) {
                $this->outputForFile(
                    $output,
                    "<error>Could not get reflection for class {$class_name} ({$e->getMessage()})</error>"
                );
                continue;
            }

            if ($this->reflection->isAbstract()) {
                $this->outputForFile(
                    $output,
                    "Skipping abstract class {$class_name}",
                    OutputInterface::VERBOSITY_VERBOSE
                );
                continue;
            }


            $model = $this->reflection->newInstance();

            // Get current properties
            $current_properties = $this->getPropertiesFromDocBlock();


            // Get configuration for class
            $config_property = $this->reflection->getProperty('config');
            $config_property->setAccessible(true);
            $model_config = $config_property->getValue()[$class_name];

            // Get table metadata
            $meta = $model->getTableMetaData();

            $properties = [];

            if (!isset($meta['fields']['id']) && count($meta['pk']) > 0) {
                $properties['id'] = [
                    'type' => count($meta['pk']) > 1 ? 'array' : $this->getPHPType($meta['pk'][0], $meta['fields'][$meta['pk'][0]]),
                    'description' => $current_properties['id']['description'] ?? 'alias for pk',
                ];
            }

            foreach ($meta['fields'] as $field => $info) {
                $name = mb_strtolower($field);
                $type = $this->getPHPType($field, $info, $model_config);
                if ($type === 'int' && isset($current_properties[$field]) && $current_properties[$field]['type'] === 'bool') {
                    $type = 'bool';
                }
                $properties[$name] = [
                    'type'        => $type,
                    'description' => $current_properties[$field]['description'] ?? 'database column',
                ];

                $alias = array_search($name, $meta['alias_fields']);
                if ($alias) {
                    $properties[$alias] = [
                        'type'        => $type,
                        'description' => $current_properties[$field]['description'] ?? "alias column for {$name}",
                    ];
                }
            }

            foreach ($meta['relations'] as $relation) {
                $options = $model->getRelationOptions($relation);
                $related_class_name = $options['class_name'];

                if (in_array($options['type'], ['has_many', 'has_and_belongs_to_many'])) {
                    $related_type = sprintf(
                        '%s<%s>',
                        $this->adjustNamespaceForClass(SimpleORMapCollection::class),
                        $this->adjustNameSpaceForClass($related_class_name),
                    );
                } else {
                    $related_type = $this->adjustNamespaceForClass($related_class_name);

                    if (
                        $options['foreign_key'] !== 'id'
                        && !is_callable($options['foreign_key'])
                        && isset($meta['fields'][$options['foreign_key']])
                        && $meta['fields'][$options['foreign_key']]['null'] === 'YES'
                    ) {
                        $related_type .= '|null';
                    } elseif (preg_match('/^find(One?)By/', $options['assoc_func'])) {
                        $related_type .= '|null';
                    }
                }

                $properties[$relation] = [
                    'type'        => $related_type,
                    'description' => $current_properties[$relation]['description'] ?? ("{$options['type']} " . $this->adjustNamespaceForClass($related_class_name)),
                ];
            }

            foreach ($meta['additional_fields'] as $field => $definition) {
                if ($field === 'id') {
                    continue;
                }

                $property_type = null;
                if (isset($definition['get']) && !isset($definition['set'])) {
                    $property_type = 'property-read';
                } elseif (!isset($definition['get']) && isset($definition['set'])) {
                    $property_type = 'property-write';
                }

                $properties[$field] = [
                    'property_type' => $property_type,
                    'type' => $current_properties[$field]['type'] ?? $this->getAdditionFieldType($definition),
                    'description' => $current_properties[$field]['description'] ?? 'additional field',
                ];
            }

            if ($this->updateDocBlockOfClass($properties)) {
                $this->outputForFile(
                    $output,
                    '<info>Updated ' . $this->relativeFilePath($file->getPathname()) . '</info>'
                );
            } else {
                $this->outputForFile(
                    $output,
                    'No changes in ' . $this->relativeFilePath($file->getPathname()),
                    OutputInterface::VERBOSITY_VERBOSE
                );
            }
        }

        $this->progress->clear();

        return Command::SUCCESS;
    }

    private function outputForFile($output, ...$args)
    {
        $this->progress->advance();
        $this->progress->clear();
        $output->writeln(...$args);
        $this->progress->display();

    }

    private function getPHPType(string $field, array $info, array $config = []): string
    {
        $field = strtolower($field);

        $type = [];

        if (isset($config['serialized_fields'][$field])) {
            $type[] = $this->adjustNamespaceForClass($config['serialized_fields'][$field]);
        } elseif (isset($config['i18n_fields'][$field])) {
            $type[] = $this->adjustNamespaceForClass(I18NString::class);
        } elseif (preg_match('/^(?:tiny|small|medium|big)?int(?:eger)?/i', $info['type'])) {
            $type[] = 'int';
        } elseif (preg_match('/^(?:decimal|double|float|numeric)/i', $info['type'])) {
            $type[] = 'float';
        } else {
            $type[] = 'string';
        }

        if ($info['null'] === 'YES') {
            $type[] = 'null';
        }

        return implode('|', $type);
    }

    private function updateDocBlockOfClass(array $properties): bool
    {
        $has_docblock = (bool) $this->reflection->getDocComment();
        $docblock     = $this->reflection->getDocComment() ?: $this->getDefaultDocblock();

        $docblock_lines = array_map('rtrim', explode("\n", $docblock));

        $properties_started = false;
        $docblock_lines = array_filter($docblock_lines, function ($line) use (&$properties_started) {
            $line = ltrim($line, '* ');
            if ($properties_started) {
                return $line === '/';
            }

            $properties_started = str_starts_with($line, '@property ');
            return !$properties_started;
        });

        $docblock_lines = array_reverse($docblock_lines);
        while ($docblock_lines[1] === ' *') {
            array_splice($docblock_lines, 1, 1);
        }
        $docblock_lines = array_reverse($docblock_lines);

        $properties = array_map(function ($variable, $property) {
            return sprintf(
                ' * @%s %s $%s %s',
                $property['property_type'] ?? 'property',
                $property['type'],
                $variable,
                $property['description']
            );
        }, array_keys($properties), array_values($properties));

        array_unshift($properties, ' *');
        array_splice($docblock_lines, -1, 0, $properties);

        $new_docblock = implode("\n", $docblock_lines);

        if ($docblock === $new_docblock) {
            return false;
        }

        $contents = file_get_contents($this->reflection->getFileName());
        if ($has_docblock) {
            $contents = str_replace($docblock, $new_docblock, $contents);
        } else {
            $contents = preg_replace(
                '/^class/m',
                $new_docblock . "\nclass",
                $contents,
                1
            );
        }

        file_put_contents($this->reflection->getFileName(), $contents);

        return true;
    }

    private function getDefaultDocBlock(): string
    {
        return implode("\n", [
            '/**',
            ' * @license GPL2 or any later version',
            ' */',
        ]);
    }

    /**
     * @see https://stackoverflow.com/a/14250011
     */
    private function getClassNameFromFile(string $filename): ?string
    {
        $code = file_get_contents($filename);
        $tokens = token_get_all($code);

        $namespace = '';

        for ($i = 0, $l = count($tokens); $i < $l; $i += 1) {
            if ($tokens[$i][0] === T_NAMESPACE) {
                for ($j= $i + 1; $j < $l; $j += 1) {
                    if ($tokens[$j][0] === T_STRING) {
                        $namespace .= "\\" . $tokens[$j][1];
                    } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
                        break;
                    }
                }
            }
            if ($tokens[$i][0] === T_CLASS) {
                for ($j = $i + 1; $j < $l; $j += 1) {
                    if ($tokens[$j] === '{') {
                        return ltrim($namespace . "\\" . $tokens[$i + 2][1], "\\");
                    }
                }
            }
        }
        return null;
    }

    private function adjustNamespaceForClass(string $class): string
    {
        if (!$this->reflection->inNamespace()) {
            return $class;
        }

        $namespace = $this->reflection->getNamespaceName();
        $class = "\\{$class}";
        if (str_starts_with($class, "\\{$namespace}")) {
            $class = substr($class, strlen($namespace) + 2);
        }
        return $class;
    }

    private function getAdditionFieldType($definition): string
    {
        // TODO: Try to get a reasonable field type
        // $definition may either be an array with definition or a boolean value

        return 'mixed';
    }

    private function getPropertiesFromDocBlock(): array
    {
        $docblock = $this->reflection->getDocComment();

        if (!trim($docblock)) {
            return [];
        }

        $config = new ParserConfig([]);
        $lexer = new Lexer($config);
        $constExprParser = new ConstExprParser($config);
        $typeParser = new TypeParser($config, $constExprParser);
        $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);

        $tokens = new TokenIterator($lexer->tokenize($docblock));
        $parsed = $phpDocParser->parse($tokens);

        $properties = [
            ...$parsed->getPropertyTagValues(),
            ...$parsed->getPropertyReadTagValues(),
            ...$parsed->getPropertyWriteTagValues(),
        ];

        $result = [];
        foreach ($properties as $property) {
            $key = substr($property->propertyName, 1);
            $result[$key] = [
                'type' => (string) $property->type,
                'description' => $this->cleanupDescription($property->description),
            ];
        }

        return $result;
    }

    private function cleanupDescription(string $description): ?string
    {
        if (
            in_array($description, ['database column', 'additional field', 'alias for pk'])
            || preg_match('/^(has_one|has_many|has_and_belongs_to_many|belongs_to) \\S+/', $description)
            || preg_match('/^alias column for \\S+/', $description)
        ) {
            return null;
        }

        return $description;
    }
}