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

TreePathHash::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 3
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
10 View Code Duplication
class TreePathHash 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...
11
{
12
    const MACRO_METHOD = 'treePathHash';
13
14
    /**
15
     * @var ExtensibleClassMetadata
16
     */
17
    protected $classMetadata;
18
19
    /**
20
     * @var string
21
     */
22
    protected $fieldName;
23
24
    /**
25
     * @param ExtensibleClassMetadata $classMetadata
26
     * @param string                  $fieldName
27
     */
28 2
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
29
    {
30 2
        $this->classMetadata = $classMetadata;
31 2
        $this->fieldName     = $fieldName;
32 2
    }
33
34
    /**
35
     * Enable TreePathHash
36
     */
37
    public static function enable()
38
    {
39 2
        Field::macro(self::MACRO_METHOD, function (Field $field) {
40 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...
41 2
        });
42 2
    }
43
44
    /**
45
     * Execute the build process
46
     */
47 1
    public function build()
48
    {
49 1
        $this->classMetadata->appendExtension($this->getExtensionName(), [
50 1
            'path_hash' => $this->fieldName
51 1
        ]);
52 1
    }
53
54
    /**
55
     * Return the name of the actual extension.
56
     *
57
     * @return string
58
     */
59 1
    public function getExtensionName()
60
    {
61 1
        return FluentDriver::EXTENSION_NAME;
62
    }
63
}
64