Passed
Push — master ( f28038...9f4251 )
by Gabriel
04:16 queued 11s
created

checkFieldsIsInitiated()   A

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 1
    public function getFields()
22
    {
23 1
        $this->checkFieldsIsInitiated();
24
25 1
        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 1
    protected function checkFieldsIsInitiated()
29
    {
30 1
        if ($this->fields === null) {
31 1
            $this->initFields();
32
        }
33 1
    }
34
35 1
    public function initFields()
36
    {
37 1
        $structure = $this->getTableStructure();
38 1
        $this->fields = array_keys($structure['fields']);
39 1
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45
    public function hasField(string $name)
46
    {
47
        $this->checkFieldsIsInitiated();
48
        return isset($this->fields[$name]);
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54 4
    protected function getTableStructure()
55
    {
56 4
        if ($this->tableStructure == null) {
57
            $this->initTableStructure();
58
        }
59
60 4
        return $this->tableStructure;
61
    }
62
63
    /**
64
     * @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...
65
     */
66 5
    public function setTableStructure($tableStructure)
67
    {
68 5
        $this->tableStructure = $tableStructure;
69 5
    }
70
71
    protected function initTableStructure()
72
    {
73
        $this->setTableStructure($this->getDB()->getMetadata()->describeTable($this->getTable()));
0 ignored issues
show
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

73
        $this->setTableStructure($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...
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

73
        $this->setTableStructure($this->/** @scrutinizer ignore-call */ getDB()->getMetadata()->describeTable($this->getTable()));
Loading history...
74
    }
75
}
76