Completed
Branch feature/pre-split (1fb89d)
by Anton
03:12
created

ReferenceSchema   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 42
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sqlStatement() 0 15 1
A createInstance() 14 14 1

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\SQLite\Schemas;
10
11
use Spiral\Database\Entities\Driver;
12
use Spiral\Database\Schemas\Prototypes\AbstractReference;
13
14
class ReferenceSchema extends AbstractReference
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function sqlStatement(Driver $driver): string
20
    {
21
        $statement = [];
22
23
        $statement[] = 'FOREIGN KEY';
24
        $statement[] = '(' . $driver->identifier($this->column) . ')';
25
26
        $statement[] = 'REFERENCES ' . $driver->identifier($this->foreignTable);
27
        $statement[] = '(' . $driver->identifier($this->foreignKey) . ')';
28
29
        $statement[] = "ON DELETE {$this->deleteRule}";
30
        $statement[] = "ON UPDATE {$this->updateRule}";
31
32
        return implode(' ', $statement);
33
    }
34
35
    /**
36
     * @param string $table
37
     * @param array  $schema
38
     *
39
     * @return ReferenceSchema
40
     */
41 View Code Duplication
    public static function createInstance(string $table, array $schema): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43
        $reference = new self($table, $schema['id']);
44
45
        $reference->column = $schema['from'];
46
47
        $reference->foreignTable = $schema['table'];
48
        $reference->foreignKey = $schema['to'];
49
50
        $reference->deleteRule = $schema['on_delete'];
51
        $reference->updateRule = $schema['on_update'];
52
53
        return $reference;
54
    }
55
}
56