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

TreeParent   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A enable() 10 10 1
A build() 6 6 1
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\Tree\Mapping\Driver\Fluent as FluentDriver;
6
use LaravelDoctrine\Fluent\Buildable;
7
use LaravelDoctrine\Fluent\Builders\Field;
8
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
9
use LaravelDoctrine\Fluent\Relations\ManyToOne;
10
11 View Code Duplication
class TreeParent 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...
12
{
13
    const MACRO_METHOD = 'treeParent';
14
15
    /**
16
     * @var ExtensibleClassMetadata
17
     */
18
    protected $classMetadata;
19
20
    /**
21
     * @var string
22
     */
23
    protected $fieldName;
24
25
    /**
26
     * @param ExtensibleClassMetadata $classMetadata
27
     * @param string                  $fieldName
28
     */
29 3
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
30
    {
31 3
        $this->classMetadata = $classMetadata;
32 3
        $this->fieldName     = $fieldName;
33 3
    }
34
35
    /**
36
     * Enable TreeParent
37
     */
38 3
    public static function enable()
39
    {
40
        Field::macro(self::MACRO_METHOD, function (Field $field) {
41 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...
42 3
        });
43
44 3
        ManyToOne::macro(self::MACRO_METHOD, function (ManyToOne $relation) {
45 1
            return new static($relation->getClassMetadata(), $relation->getRelation());
0 ignored issues
show
Compatibility introduced by
$relation->getClassMetadata() of type object<Doctrine\ORM\Mapping\ClassMetadata> is not a sub-type of object<LaravelDoctrine\F...xtensibleClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadata 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...
46 3
        });
47 3
    }
48
49
    /**
50
     * Execute the build process
51
     */
52 1
    public function build()
53
    {
54 1
        $this->classMetadata->appendExtension($this->getExtensionName(), [
55 1
            'parent' => $this->fieldName
56 1
        ]);
57 1
    }
58
59
    /**
60
     * Return the name of the actual extension.
61
     *
62
     * @return string
63
     */
64 1
    public function getExtensionName()
65
    {
66 1
        return FluentDriver::EXTENSION_NAME;
67
    }
68
}
69