Completed
Push — extensions-support ( 74d037...c14d66 )
by Patrick
03:15
created

TreeLeft   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 58
loc 58
rs 10
ccs 17
cts 17
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A enable() 6 6 1
A build() 10 10 2
A getExtensionName() 4 4 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
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Exception\InvalidMappingException;
6
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver;
7
use Gedmo\Tree\Mapping\Validator;
8
use LaravelDoctrine\Fluent\Buildable;
9
use LaravelDoctrine\Fluent\Builders\Field;
10
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
11
12 View Code Duplication
class TreeLeft implements Buildable
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    const MACRO_METHOD = 'treeLeft';
15
16
    /**
17
     * @var ExtensibleClassMetadata
18
     */
19
    protected $classMetadata;
20
21
    /**
22
     * @var string
23
     */
24
    protected $fieldName;
25
26
    /**
27
     * @param ExtensibleClassMetadata $classMetadata
28
     * @param string                  $fieldName
29
     */
30 3
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
31
    {
32 3
        $this->classMetadata = $classMetadata;
33 3
        $this->fieldName     = $fieldName;
34 3
    }
35
36
    /**
37
     * Enable TreeLeft
38
     */
39
    public static function enable()
40
    {
41 2
        Field::macro(self::MACRO_METHOD, function (Field $field) {
42 1
            return new static($field->getClassMetadata(), $field->getName());
0 ignored issues
show
Compatibility introduced by
$field->getClassMetadata() of type object<Doctrine\ORM\Mapping\ClassMetadataInfo> is not a sub-type of object<LaravelDoctrine\F...xtensibleClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadataInfo to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
43 2
        });
44 2
    }
45
46
    /**
47
     * Execute the build process
48
     */
49 2
    public function build()
50
    {
51 2
        if (!(new Validator())->isValidField($this->classMetadata, $this->fieldName)) {
52 1
            throw new InvalidMappingException("Tree left field must be 'integer' in class - {$this->classMetadata->name}");
53
        }
54
55 1
        $this->classMetadata->appendExtension($this->getExtensionName(), [
56 1
            'left' => $this->fieldName
57 1
        ]);
58 1
    }
59
60
    /**
61
     * Return the name of the actual extension.
62
     *
63
     * @return string
64
     */
65 1
    public function getExtensionName()
66
    {
67 1
        return FluentDriver::EXTENSION_NAME;
68
    }
69
}
70