Test Setup Failed
Push — master ( c9a16f...d6cc86 )
by Oliver
09:18
created

FluentMeta::existsOnParent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4286
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of Mailable.
5
 *
6
 * (c) Oliver Green <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BoxedCode\Eloquent\Meta;
13
14
trait FluentMeta
15
{
16
    use Metable;
17
18
    /**
19
     * Model table columns.
20
     *
21
     * @var array
22
     */
23
    public static $model_table_columns = [];
24
25
    /**
26
     * Get a models table columns.
27
     *
28
     * @param $class
29
     * @return mixed
30
     */
31
    protected function getClassColumns($class)
32
    {
33
        if (! isset(static::$model_table_columns[$class])) {
34
            static::$model_table_columns[$class] = $this->getConnection()
1 ignored issue
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
                ->getSchemaBuilder()
36
                ->getColumnListing($this->getTable());
1 ignored issue
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
        }
38
39
        return static::$model_table_columns[$class];
40
    }
41
42
    /**
43
     * Check whether a method, property or attribute
44
     * name exists on the model.
45
     *
46
     * @param $name
47
     * @return bool
48
     */
49
    protected function existsOnParent($name)
50
    {
51
        $columns = $this->getClassColumns(static::class);
52
53
        return property_exists($this, $name)
54
            || method_exists($this, $name)
55
            || in_array($name, $columns);
56
    }
57
58
    /**
59
     * Dynamically determine whether a meta item or model property isset.
60
     *
61
     * @param $name
62
     * @return bool
63
     */
64
    public function __isset($name)
65
    {
66
        if ($this->existsOnParent($name)) {
67
            return parent::__isset($name);
68
        }
69
70
        return isset($this->meta->$name);
1 ignored issue
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Eloquent\Meta\FluentMeta>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
    }
72
73
    /**
74
     * Dynamically get a model property / attribute or meta item.
75
     *
76
     * @param $name
77
     * @return mixed
78
     */
79
    public function __get($name)
80
    {
81
        if ($this->existsOnParent($name)) {
82
            return parent::__get($name);
83
        }
84
85
        if ($meta = $this->meta->$name) {
1 ignored issue
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Eloquent\Meta\FluentMeta>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
$meta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
            return $this->meta->$name;
1 ignored issue
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Eloquent\Meta\FluentMeta>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
        }
88
    }
89
90
    /**
91
     * Dynamically set a meta item or model property / attribute.
92
     *
93
     * @param $name
94
     * @param $value
95
     * @return mixed
96
     */
97
    public function __set($name, $value)
98
    {
99
        if ($this->existsOnParent($name)) {
100
            return parent::__set($name, $value);
101
        }
102
103
        $this->meta->$name = $value;
1 ignored issue
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Eloquent\Meta\FluentMeta>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
    }
105
106
    /**
107
     * Dynamically unset a meta item or model property / attribute.
108
     *
109
     * @param $name
110
     */
111
    public function __unset($name)
112
    {
113
        if ($this->existsOnParent($name)) {
114
            unset($this->$name);
115
116
            return;
117
        }
118
119
        if ($meta = $this->meta->$name) {
1 ignored issue
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Eloquent\Meta\FluentMeta>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
$meta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
120
            unset($this->meta->$name);
121
        }
122
    }
123
}
124