Completed
Push — master ( 8fe865 )
by Dmitry
04:10
created

UserClasses   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 28
loc 56
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepositoryClass() 0 6 2
A getEntityClass() 0 6 2
A mapEntity() 14 14 3
A mapRepository() 14 14 3
A validateSpace() 0 6 2

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 Tarantool\Mapper\Plugins;
4
5
use Exception;
6
use Tarantool\Mapper\Entity;
7
use Tarantool\Mapper\Plugin;
8
use Tarantool\Mapper\Repository;
9
use Tarantool\Mapper\Space;
10
11
class UserClasses extends Plugin
12
{
13
    private $repositories = [];
14
    private $entities = [];
15
16
    public function getRepositoryClass(Space $space)
17
    {
18
        if(array_key_exists($space->getName(), $this->repositories)) {
19
            return $this->repositories[$space->getName()];
20
        }
21
    }
22
23
    public function getEntityClass(Space $space)
24
    {
25
        if(array_key_exists($space->getName(), $this->entities)) {
26
            return $this->entities[$space->getName()];
27
        }
28
    }
29
30 View Code Duplication
    public function mapEntity($space, $class)
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...
31
    {
32
        $this->validateSpace($space);
33
34
        if(!class_exists($class)) {
35
            throw new Exception("No class $class");
36
        }
37
38
        if(!is_subclass_of($class, Entity::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Entity::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
39
            throw new Exception("Entity should extend " . Entity::class . " class");
40
        }
41
42
        $this->entities[$space] = $class;
43
    }
44
45 View Code Duplication
    public function mapRepository($space, $class)
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...
46
    {
47
        $this->validateSpace($space);
48
49
        if(!class_exists($class)) {
50
            throw new Exception("No class $class");
51
        }
52
53
        if(!is_subclass_of($class, Repository::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Repository::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
54
            throw new Exception("Repository should extend " . Repository::class . " class");
55
        }
56
57
        $this->repositories[$space] = $class;
58
    }
59
60
    public function validateSpace($space)
61
    {
62
        if(!$this->mapper->getSchema()->hasSpace($space)) {
63
            throw new Exception("No space $space");
64
        }
65
    }
66
}
67