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

Command::getAttributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 5
c 2
b 1
f 1
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
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' => 'Controller 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
            'controllerClass' => 'This is the name of the controller class to be generated. You should
80
                provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>),
81
                and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class
82
                is using the same namespace as specified by your application\'s namespace property.',
83
            'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
84
        ];
85
    }
86
87
    public static function getAttributes(): array
88
    {
89
        return [
90
            'namespace',
91
            'tableName',
92
            'baseClass',
93
            'template',
94
        ];
95
    }
96
}
97