PositionEnhancer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enhance() 0 25 4
A supports() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Description;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use PHPCR\PathNotFoundException;
18
use PHPCR\Util\PathHelper;
19
use Symfony\Cmf\Component\Resource\Description\Description;
20
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
21
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource;
22
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
23
24
/**
25
 * A description enhancer to add the position/sorting value of children on a parent.
26
 *
27
 * @author Maximilian Berghoff <[email protected]>
28
 */
29
class PositionEnhancer implements DescriptionEnhancerInterface
30
{
31
    /**
32
     * @var \PHPCR\SessionInterface
33
     */
34
    private $session;
35
36
    /**
37
     * @param $sessionName
38
     */
39
    public function __construct(ManagerRegistry $manager, $sessionName)
40
    {
41
        $this->session = $manager->getConnection($sessionName);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function enhance(Description $description)
48
    {
49
        $nodePath = $description->getResource()->getPath();
50
        $nodeName = PathHelper::getNodeName($nodePath);
51
        $parentPath = PathHelper::getParentPath($nodePath);
52
53
        try {
54
            $parentNode = $this->session->getNode($parentPath);
55
        } catch (PathNotFoundException $exception) {
56
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Symfony\Cmf\Component\Re...ancerInterface::enhance of type Symfony\Cmf\Component\Re...Description\Description.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
57
        }
58
59
        $nodeIterator = $parentNode->getNodes();
60
        $nodeIterator->rewind();
61
        $counter = 0;
62
        while ($nodeIterator->valid()) {
63
            ++$counter;
64
            if ($nodeIterator->key() === $nodeName) {
65
                break;
66
            }
67
            $nodeIterator->next();
68
        }
69
70
        $description->set('position', $counter);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supports(PuliResource $resource)
77
    {
78
        if (!$resource instanceof CmfResource) {
79
            return false;
80
        }
81
82
        try {
83
            $parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath()));
0 ignored issues
show
Unused Code introduced by
$parentNode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        } catch (PathNotFoundException $exception) {
85
            return false;
86
        }
87
88
        // Todo: check for non orderable type
89
90
        return true;
91
    }
92
}
93