Completed
Push — master ( edee30...341f62 )
by Oscar
02:21
created

FieldFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 147
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 19
Bugs 3 Features 0
Metric Value
wmc 14
c 19
b 3
f 0
lcom 2
cbo 2
dl 7
loc 147
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addNamespace() 0 6 1
A mapNames() 0 6 1
A mapRegex() 0 6 1
A mapTypes() 0 6 1
B get() 7 21 5
B getClassName() 0 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        'file' => 'File',
18
    ];
19
20
    protected $regexMap = [
21
        //relation fields (post_id)
22
        '/_id$/' => 'Integer',
23
24
        //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...
25
        '/^(is|has)[A-Z]/' => 'Boolean',
26
27
        //time related (createdAt, publishedAt)
28
        '/[a-z]At$/' => 'Datetime',
29
30
        //time related (createdAt, publishedAt)
31
        '/[a-z]File$/' => 'File',
32
    ];
33
34
    protected $typeMap = [
35
        'bigint' => 'Integer',
36
        'boolean' => 'Boolean',
37
        'date' => 'Date',
38
        'datetime' => 'Datetime',
39
        'float' => 'Decimal',
40
        'mediumint' => 'Integer',
41
        'set' => 'Set',
42
        'smallint' => 'Integer',
43
        'tinyint' => 'Integer',
44
        'year' => 'Integer',
45
    ];
46
47
    /**
48
     * Set the namespace for the fields classes.
49
     *
50
     * @param string $namespace
51
     *
52
     * @return self
53
     */
54
    public function addNamespace($namespace)
55
    {
56
        array_unshift($this->namespaces, $namespace);
57
58
        return $this;
59
    }
60
61
    /**
62
     * Map names with field types.
63
     *
64
     * @param array $map
65
     *
66
     * @return self
67
     */
68
    public function mapNames(array $map)
69
    {
70
        $this->nameMap = $map + $this->nameMap;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Map names with field types using regexp.
77
     *
78
     * @param array $map
79
     *
80
     * @return self
81
     */
82
    public function mapRegex(array $map)
83
    {
84
        $this->regexMap = $map + $this->regexMap;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Map db field types with classes.
91
     *
92
     * @param array $map
93
     *
94
     * @return self
95
     */
96
    public function mapTypes(array $map)
97
    {
98
        $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...
99
100
        return $this;
101
    }
102
103
    /**
104
     * @see FieldFactoryInterface
105
     *
106
     * {@inheritdoc}
107
     */
108
    public function get(Table $table, $name)
109
    {
110
        $scheme = $table->getScheme()['fields'];
111
112
        if (!isset($scheme[$name])) {
113
            throw new SimpleCrudException("The field '{$name}' does not exist in the table {$table->name}");
114
        }
115
116
117
        $className = $this->getClassName($name, $scheme[$name]['type']) ?: $this->defaultType;
118
119 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...
120
            $class = $namespace.$className;
121
122
            if (class_exists($class)) {
123
                return new $class($table, $name);
124
            }
125
        }
126
127
        throw new SimpleCrudException("No field class found for '{$className}'");
128
    }
129
130
    /**
131
     * Get the field class name.
132
     *
133
     * @param string $name
134
     * @param string $type
135
     *
136
     * @return string|null
137
     */
138
    protected function getClassName($name, $type)
139
    {
140
        if (isset($this->nameMap[$name])) {
141
            return $this->nameMap[$name];
142
        }
143
144
        foreach ($this->regexMap as $regex => $class) {
145
            if (preg_match($regex, $name)) {
146
                return $class;
147
            }
148
        }
149
150
        if (isset($this->typeMap[$type])) {
151
            return $this->typeMap[$type];
152
        }
153
    }
154
}
155