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

Command::getModelName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
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\TemplateRule;
13
14
final class Command extends AbstractGeneratorCommand
15
{
16
    public function __construct(
17
        #[Required]
18
        #[Regex(
19
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
20
            message: 'Invalid namespace'
21
        )]
22
        private readonly string $namespace = 'App\\Model',
23
        #[Required]
24
        #[Regex(
25
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
26
            message: 'Invalid table name'
27
        )]
28
        private readonly string $tableName = 'user',
29
        #[Regex(
30
            pattern: '/^[a-z\\\\]*$/i',
31
            message: 'Only word characters and backslashes are allowed.',
32
            skipOnEmpty: true,
33
        )]
34
        private readonly string $baseClass = ActiveRecord::class,
35
        #[Required(message: 'A code template must be selected.')]
36
        #[TemplateRule]
37
        protected string $template = 'default',
38
    ) {
39
        parent::__construct($template);
40
    }
41
42
    public function getNamespace(): string
43
    {
44
        return $this->namespace;
45
    }
46
47
    public function getBaseClass(): string
48
    {
49
        return $this->baseClass;
50
    }
51
52
    public function getTableName(): string
53
    {
54
        return $this->tableName;
55
    }
56
57
    public function getModelName(): string
58
    {
59
        return (new Inflector())->tableToClass($this->tableName);
60
    }
61
62
    public static function getAttributeLabels(): array
63
    {
64
        return [
65
            'namespace' => 'Controller Namespace',
66
            'tableName' => 'Table name',
67
            'template' => 'Action IDs',
68
        ];
69
    }
70
71
    public static function getHints(): array
72
    {
73
        return [
74
            'controllerClass' => 'This is the name of the controller class to be generated. You should
75
                provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>),
76
                and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class
77
                is using the same namespace as specified by your application\'s namespace property.',
78
            'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
79
        ];
80
    }
81
82
    public static function getAttributes(): array
83
    {
84
        return [
85
            'namespace',
86
            'tableName',
87
            'template',
88
        ];
89
    }
90
}
91