Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

src/Description/PositionEnhancer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Sonata\DoctrinePHPCRAdminBundle\Description;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use PHPCR\PathNotFoundException;
7
use PHPCR\Util\PathHelper;
8
use Symfony\Cmf\Component\Resource\Description\Description;
9
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
10
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource;
11
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
12
13
/**
14
 * A description enhancer to add the position/sorting value of children on a parent.
15
 *
16
 * @author Maximilian Berghoff <[email protected]>
17
 */
18
class PositionEnhancer implements DescriptionEnhancerInterface
19
{
20
    /**
21
     * @var \PHPCR\SessionInterface
22
     */
23
    private $session;
24
25
    /**
26
     * @param ManagerRegistry $manager
27
     * @param $sessionName
28
     */
29
    public function __construct(ManagerRegistry $manager, $sessionName)
30
    {
31
        $this->session = $manager->getConnection($sessionName);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function enhance(Description $description)
38
    {
39
        $nodePath = $description->getResource()->getPath();
40
        $nodeName = PathHelper::getNodeName($nodePath);
41
        $parentPath = PathHelper::getParentPath($nodePath);
42
43
        try {
44
            $parentNode = $this->session->getNode($parentPath);
45
        } catch (PathNotFoundException $exception) {
46
            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...
47
        }
48
49
        $nodeIterator = $parentNode->getNodes();
50
        $nodeIterator->rewind();
51
        $counter = 0;
52
        while ($nodeIterator->valid()) {
53
            ++$counter;
54
            if ($nodeIterator->key() === $nodeName) {
55
                break;
56
            }
57
            $nodeIterator->next();
58
        }
59
60
        $description->set('position', $counter);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supports(PuliResource $resource)
67
    {
68
        if (!$resource instanceof CmfResource) {
69
            return false;
70
        }
71
72
        try {
73
            $parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath()));
0 ignored issues
show
$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...
74
        } catch (PathNotFoundException $exception) {
75
            return false;
76
        }
77
78
        // Todo: check for non orderable type
79
80
        return true;
81
    }
82
}
83