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

TableStructMiddleware::rewriteFromClassDoc()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 7
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\middleware\ObjectMiddleware;
8
use suda\orm\struct\TableClassStructBuilder;
9
10
/**
11
 * 结构中间件
12
 */
13
class TableStructMiddleware extends ObjectMiddleware
14
{
15
    /**
16
     * 数据结构
17
     *
18
     * @var TableStruct
19
     */
20
    protected $struct;
21
22
    /**
23
     * 创建中间件
24
     *
25
     * @param string $object
26
     */
27
    public function __construct(string $object, TableStruct $struct)
28
    {
29
        $this->object = $object;
30
        $this->struct = $struct;
31
        $this->prepareProcessorSet($object);
32
    }
33
34
    /**
35
     * 处理输入字段名
36
     */
37
    public function inputName(string $name):string
38
    {
39
        return $this->struct->getFields()->inputName($name);
40
    }
41
42
    /**
43
     * 处理输出字段名
44
     *
45
     * @param string $name
46
     * @param mixed $data
47
     * @return mixed
48
     */
49
    public function outputName(string $name):string
50
    {
51
        return $this->struct->getFields()->outputName($name);
52
    }
53
    
54
    /**
55
     * 创建处理集合
56
     *
57
     * @param string $object
58
     * @return void
59
     */
60
    protected function prepareProcessorSet(string $object)
61
    {
62
        $reflectObject = new ReflectionClass($object);
63
        $classDoc = $reflectObject->getDocComment()?:'';
64
        $field = TableClassStructBuilder::readClassDocField($classDoc);
0 ignored issues
show
Bug introduced by
It seems like $classDoc can also be of type true; however, parameter $classDoc of suda\orm\struct\TableCla...er::readClassDocField() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $field = TableClassStructBuilder::readClassDocField(/** @scrutinizer ignore-type */ $classDoc);
Loading history...
Bug introduced by
Are you sure the assignment to $field is correct as suda\orm\struct\TableCla...lassDocField($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...
65
        if ($field !== null) {
0 ignored issues
show
introduced by
The condition $field !== null is always false.
Loading history...
66
            $this->createProccorFromStruct();
67
        } else {
68
            $this->createProccorFromClass();
69
        }
70
        $this->rewriteFromClassDoc($classDoc);
0 ignored issues
show
Bug introduced by
It seems like $classDoc can also be of type true; however, parameter $classDoc of suda\orm\struct\TableStr...::rewriteFromClassDoc() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $this->rewriteFromClassDoc(/** @scrutinizer ignore-type */ $classDoc);
Loading history...
71
    }
72
73
    protected function createProccorFromStruct()
74
    {
75
        $this->processor = [];
76
        $fields = $this->struct->getFields();
77
        foreach ($fields as $key => $value) {
78
            $this->processor[$key] = ObjectMiddleware::RAW;
79
        }
80
    }
81
82
    protected function rewriteFromClassDoc(string $classDoc)
83
    {
84
        if (\preg_match_all('/@field-serialize\s+(\w+)/i', $classDoc, $matchs)) {
85
            foreach ($matchs[0] as $index => $value) {
86
                $match = \array_column($matchs, $index);
87
                list($comment, $name) = $match;
88
                $this->processor[$name] = ObjectMiddleware::SERIALIZE;
89
            }
90
        }
91
    }
92
93
    protected function createProccorFromClass()
94
    {
95
        $this->processor = [];
96
        foreach ($reflectObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $property) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reflectObject seems to be never defined.
Loading history...
97
            $name = $this->inputName($property->getName());
98
            $this->processor[$name] = $this->getProcessorType($property);
99
        }
100
    }
101
}
102