Completed
Push — master ( c751a1...6628b2 )
by Dmitry
02:21
created

UserClasses   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 42.42 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

7 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 getRepositoryMapping() 0 4 1
A getEntityMapping() 0 4 1
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\Plugin;
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
    protected $repositoryMapping = [];
14
    protected $entityMapping = [];
15
16
    public function getRepositoryClass(Space $space)
17
    {
18
        if (array_key_exists($space->getName(), $this->repositoryMapping)) {
19
            return $this->repositoryMapping[$space->getName()];
20
        }
21
    }
22
23
    public function getEntityClass(Space $space)
24
    {
25
        if (array_key_exists($space->getName(), $this->entityMapping)) {
26
            return $this->entityMapping[$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->entityMapping[$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->repositoryMapping[$space] = $class;
58
    }
59
60
    public function getRepositoryMapping()
61
    {
62
        return $this->repositoryMapping;
63
    }
64
65
    public function getEntityMapping()
66
    {
67
        return $this->entityMapping;
68
    }
69
70
    public function validateSpace($space)
71
    {
72
        if (!$this->mapper->getSchema()->hasSpace($space)) {
73
            throw new Exception("No space $space");
74
        }
75
    }
76
}
77