Test Failed
Pull Request — master (#110)
by Dmitriy
05:39 queued 02:42
created

Command   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
eloc 20
c 3
b 1
f 1
dl 0
loc 76
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelName() 0 3 1
A getNamespace() 0 3 1
A __construct() 0 26 1
A getBaseClass() 0 3 1
A getTableName() 0 3 1
A getAttributeLabels() 0 7 1
A getAttributes() 0 7 1
A getHints() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\ActiveRecord;
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\ActiveRecord was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Yiisoft\Strings\Inflector;
9
use Yiisoft\Validator\Rule\Regex;
10
use Yiisoft\Validator\Rule\Required;
11
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
12
use Yiisoft\Yii\Gii\Validator\ClassExistsRule;
13
use Yiisoft\Yii\Gii\Validator\TableExistsRule;
14
use Yiisoft\Yii\Gii\Validator\TemplateRule;
15
16
final class Command extends AbstractGeneratorCommand
17
{
18
    public function __construct(
19
        #[Required]
20
        #[Regex(
21
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
22
            message: 'Invalid namespace'
23
        )]
24
        private readonly string $namespace = 'App\\Model',
25
        #[Required]
26
        #[Regex(
27
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
28
            message: 'Invalid table name'
29
        )]
30
        #[TableExistsRule]
31
        private readonly string $tableName = 'user',
32
        #[Regex(
33
            pattern: '/^[a-z\\\\]*$/i',
34
            message: 'Only word characters and backslashes are allowed.',
35
            skipOnEmpty: true,
36
        )]
37
        #[ClassExistsRule]
38
        private readonly string $baseClass = ActiveRecord::class,
39
        #[Required(message: 'A code template must be selected.')]
40
        #[TemplateRule]
41
        protected string $template = 'default',
42
    ) {
43
        parent::__construct($template);
44
    }
45
46
    public function getNamespace(): string
47
    {
48
        return $this->namespace;
49
    }
50
51
    public function getBaseClass(): string
52
    {
53
        return $this->baseClass;
54
    }
55
56
    public function getTableName(): string
57
    {
58
        return $this->tableName;
59
    }
60
61
    public function getModelName(): string
62
    {
63
        return (new Inflector())->tableToClass($this->tableName);
64
    }
65
66
    public static function getAttributeLabels(): array
67
    {
68
        return [
69
            'namespace' => 'Model namespace',
70
            'baseClass' => 'Base class',
71
            'tableName' => 'Table name',
72
            'template' => 'Template',
73
        ];
74
    }
75
76
    public static function getHints(): array
77
    {
78
        return [
79
            'namespace' => 'Namespace for the model class to store it in the related directory.',
80
            'baseClass' => 'Parent class for the new model class.',
81
            'tableName' => 'Corresponded table name for the model class.',
82
        ];
83
    }
84
85
    public static function getAttributes(): array
86
    {
87
        return [
88
            'namespace',
89
            'tableName',
90
            'baseClass',
91
            'template',
92
        ];
93
    }
94
}
95