Passed
Push — master ( 9bc88c...903b03 )
by 世昌
02:14
created

TableClassStructBuilder::createClassField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
use ReflectionClass;
5
use ReflectionProperty;
6
use suda\orm\TableStruct;
7
use suda\orm\struct\Field;
8
use suda\orm\struct\TableStructBuilder;
9
use suda\orm\struct\FieldModifierParser;
10
11
/**
12
 * 从类的类注释构建对象
13
 */
14
class TableClassStructBuilder extends TableStructBuilder
15
{
16
    /**
17
     * 解析对象
18
     *
19
     * @var string
20
     */
21
    protected $object;
22
23
    /**
24
     * 反射对象
25
     *
26
     * @var ReflectionClass
27
     */
28
    protected $reflectObject;
29
30
    /**
31
     * 类文档
32
     *
33
     * @var string
34
     */
35
    protected $classDoc;
36
37
    public function __construct(string $object)
38
    {
39
        $this->object = $object;
40
        $this->reflectObject = new ReflectionClass($object);
41
        $this->classDoc = $this->reflectObject->getDocComment() ?: '';
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->reflectObject->getDocComment() ?: '' can also be of type true. However, the property $classDoc is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
    }
43
44
45
    /**
46
     * 创建表结构
47
     *
48
     * @return TableStruct
49
     */
50
    public function createStruct():TableStruct
51
    {
52
        if (($match = static::readClassDocField($this->classDoc)) !== null) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $match is correct as static::readClassDocField($this->classDoc) targeting suda\orm\struct\TableCla...er::readClassDocField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition $match = static::readCla...his->classDoc) !== null is always false.
Loading history...
53
            return $this->createClassTableStruct($match);
54
        }
55
        return parent::createStruct();
56
    }
57
58
    /**
59
     * 判断类文档注释是否包含字段
60
     *
61
     * @param strring $classDoc
0 ignored issues
show
Bug introduced by
The type suda\orm\struct\strring 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...
62
     * @return array|null
63
     */
64
    public static function readClassDocField(string $classDoc):?array
65
    {
66
        if (preg_match_all('/@field\s+(\w+)\s+(\w+)(?:\((.+?)\))?\s+(.+)?$/im', $classDoc, $match)) {
67
            return $match;
68
        }
69
        return null;
70
    }
71
72
 
73
    /**
74
     * 创建表结构
75
     *
76
     * @param array $fields
77
     * @return \suda\orm\TableStruct
78
     */
79
    protected function createClassTableStruct(array $fields): TableStruct
80
    {
81
        $name = $this->getName();
82
        $struct = new TableStruct($name);
83
        foreach ($fields[0] as $index => $value) {
84
            $match = \array_column($fields, $index);
85
            list($comment, $field, $type, $length, $modifier) = $match;
86
            $fieldObj = $this->createClassField($name, trim($field), trim($type), static::parseLength($length), trim($modifier));
87
            $struct->addField($fieldObj);
88
        }
89
        return $struct;
90
    }
91
92
    /**
93
     * 创建字段
94
     *
95
     * @param string $tableName
96
     * @param string $name
97
     * @param string $type
98
     * @param string|array|null $length
99
     * @param string $modifier
100
     * @return \suda\orm\struct\Field
101
     */
102
    protected function createClassField(string $tableName, string $name, string $type, $length, string $modifier): Field
103
    {
104
        $parser = new FieldModifierParser;
105
        $field = new Field($tableName, $name, $type, $length);
106
        $parser->parse($modifier)->modify($field);
107
        return $field;
108
    }
109
}
110