checkFieldsIsInitiated()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\Records\Traits\TableStructure;
4
5
/**
6
 * Trait TableStructureRecordsTrait
7
 * @package Nip\Records\Traits\TableStructure
8
 */
9
trait TableStructureRecordsTrait
10
{
11
    protected $tableStructure = null;
12
13
    /**
14
     * @var null|array
15
     */
16
    protected $fields = null;
17
18
    /**
19
     * @return null
20
     */
21 4
    public function getFields()
22
    {
23 4
        $this->checkFieldsIsInitiated();
24
25 4
        return $this->fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fields returns the type array which is incompatible with the documented return type null.
Loading history...
26
    }
27
28
    /**
29
     * @param array|null $fields
30
     */
31 2
    public function setFields(?array $fields): void
32
    {
33 2
        $this->fields = $fields;
34 2
    }
35
36 4
    protected function checkFieldsIsInitiated()
37
    {
38 4
        if ($this->fields === null) {
39 2
            $this->initFields();
40
        }
41 4
    }
42
43 2
    public function initFields()
44
    {
45 2
        $structure = $this->getTableStructure();
46 2
        $this->setFields(array_keys($structure['fields']));
47 2
    }
48
49
    /**
50
     * @param string $name
51
     * @return bool
52
     */
53
    public function hasField(string $name)
54
    {
55
        $this->checkFieldsIsInitiated();
56
        return isset($this->fields[$name]);
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62 5
    public function getTableStructure()
63
    {
64 5
        if ($this->tableStructure == null) {
65 2
            $this->initTableStructure();
66
        }
67
68 5
        return $this->tableStructure;
69
    }
70
71
    /**
72
     * @param null $tableStructure
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tableStructure is correct as it would always require null to be passed?
Loading history...
73
     */
74 7
    public function setTableStructure($tableStructure)
75
    {
76 7
        $this->tableStructure = $tableStructure;
77 7
    }
78
79 2
    protected function initTableStructure()
80
    {
81 2
        $this->setTableStructure($this->generateTableStructure());
82 2
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    protected function generateTableStructure()
88
    {
89
        return $this->getDB()->getMetadata()->describeTable($this->getTable());
0 ignored issues
show
Bug introduced by
It seems like getDB() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

89
        return $this->/** @scrutinizer ignore-call */ getDB()->getMetadata()->describeTable($this->getTable());
Loading history...
Bug introduced by
The method getTable() does not exist on Nip\Records\Traits\Table...leStructureRecordsTrait. Did you maybe mean getTableStructure()? ( Ignorable by Annotation )

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

89
        return $this->getDB()->getMetadata()->describeTable($this->/** @scrutinizer ignore-call */ getTable());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
    }
91
}
92