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

TreeRoot::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
ccs 7
cts 7
cp 1
cc 1
eloc 5
nc 1
nop 0
crap 1
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 TreeRoot 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 = 'treeRoot';
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 TreeRoot
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
            'root' => $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