Passed
Branch feature-validator (0d7506)
by Thomas
02:52
created

Error   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 42
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
1
<?php
2
3
namespace ORM\Dbal;
4
5
use ORM\Dbal\Column;
6
use ORM\Namer;
7
8
abstract class Error
9
{
10
    const ERROR_CODE = 'UNKNOWN';
11
12
    /** @var string */
13
    protected $code = self::ERROR_CODE;
14
15
    /** @var string */
16
    protected $message = '%value% is not valid for column %column% from type %type%';
17
18
    /** @var mixed */
19
    protected $value;
20
21
    /** @var Column */
22
    public $column;
23
24
    /**
25
     * Error constructor.
26
     *
27
     * @param Column $column
28
     */
29 2
    public function __construct(Column $column, $value = null, $code = null, $message = null)
30
    {
31 2
        $this->column = $column;
32 2
        $this->value = $value;
33 2
        $this->code = static::ERROR_CODE;
34
35 2
        $template = new Namer();
36 2
        $this->message = $template->substitute(
37 2
            $message ? $message : $this->message,
38
            [
39 2
                'value' => (string)$value,
40 2
                'column' => $column->name,
41 2
                'type' => get_class($column->type)
0 ignored issues
show
Documentation introduced by
The property $type is declared protected in ORM\Dbal\Column. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
42
            ]
43
        );
44
45 2
        if ($code) {
46 1
            $this->code = $code;
47
        }
48 2
    }
49
}
50