Completed
Push — master ( 0eeb04...4002ae )
by Oscar
02:31
created

FieldFactory::getClassName()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 8
nc 6
nop 2
1
<?php
2
3
namespace SimpleCrud;
4
5
/**
6
 * Class to create instances of fields.
7
 */
8
class FieldFactory implements FieldFactoryInterface
9
{
10
    protected $namespaces = ['SimpleCrud\\Fields\\'];
11
    protected $defaultType = 'Field';
12
13
    protected $nameMap = [
14
        'id' => 'Integer',
15
        'active' => 'Boolean',
16
        'pubdate' => 'Datetime',
17
    ];
18
19
    protected $regexMap = [
20
        //relation fields (post_id)
21
        '/_id$/' => 'Integer',
22
23
        //flags (isActive, inHome)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
        '/^(is|has)[A-Z]/' => 'Boolean',
25
26
        //time related (createdAt, publishedAt)
27
        '/[a-z]At$/' => 'Datetime',
28
    ];
29
30
    protected $typeMap = [
31
        'bigint' => 'Integer',
32
        'boolean' => 'Boolean',
33
        'date' => 'Date',
34
        'datetime' => 'Datetime',
35
        'float' => 'Decimal',
36
        'mediumint' => 'Integer',
37
        'set' => 'Set',
38
        'smallint' => 'Integer',
39
        'tinyint' => 'Integer',
40
        'year' => 'Integer',
41
    ];
42
43
    /**
44
     * Set the namespace for the fields classes.
45
     *
46
     * @param string $namespace
47
     *
48
     * @return self
49
     */
50
    public function addNamespace($namespace)
51
    {
52
        array_unshift($this->namespaces, $namespace);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Map names with field types.
59
     *
60
     * @param array $map
61
     *
62
     * @return self
63
     */
64
    public function mapNames(array $map)
65
    {
66
        $this->mapNames = $map + $this->mapNames;
0 ignored issues
show
Bug introduced by
The property mapNames does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
68
        return $this;
69
    }
70
71
    /**
72
     * Map names with field types using regexp.
73
     *
74
     * @param array $map
75
     *
76
     * @return self
77
     */
78
    public function mapRegex(array $map)
79
    {
80
        $this->regexMap = $map + $this->regexMap;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Map db field types with classes.
87
     *
88
     * @param array $map
0 ignored issues
show
Bug introduced by
There is no parameter named $map. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
     *
90
     * @return self
91
     */
92
    public function mapTypes(array $types)
0 ignored issues
show
Unused Code introduced by
The parameter $types is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        $this->mapTypes = $map + $this->mapTypes;
0 ignored issues
show
Bug introduced by
The property mapTypes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $map does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
96
        return $this;
97
    }
98
99
    /**
100
     * @see TableFactoryInterface
101
     *
102
     * {@inheritdoc}
103
     */
104
    public function get(Table $table, $name)
105
    {
106
        $scheme = $table->getScheme()['fields'];
107
108
        if (!isset($scheme[$name])) {
109
            throw new SimpleCrudException("The field '{$name}' does not exist in the table {$table->name}");
110
        }
111
112
        $className = $this->getClassName($name, $scheme[$name]['type']) ?: $this->defaultType;
113
114 View Code Duplication
        foreach ($this->namespaces as $namespace) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            $class = $namespace.$className;
116
117
            if (class_exists($class)) {
118
                return new $class($table, $name);
119
            }
120
        }
121
122
        throw new SimpleCrudException("No field class found for '{$name}'");
123
    }
124
125
    /**
126
     * Get the field class name.
127
     *
128
     * @param string $name
129
     * @param string $type
130
     *
131
     * @return string|null
132
     */
133
    protected function getClassName($name, $type)
134
    {
135
        if (isset($this->nameMap[$name])) {
136
            return $this->nameMap[$name];
137
        }
138
139
        foreach ($this->regexMap as $regex => $class) {
140
            if (preg_match($regex, $name)) {
141
                return $class;
142
            }
143
        }
144
145
        if (isset($this->typeMap[$type])) {
146
            return $this->typeMap[$type];
147
        }
148
    }
149
}
150