Completed
Push — master ( 22f94e...b6697a )
by Jordi Sala
01:48
created

Resources/DataFixtures/Phpcr/LoadTreeData.php (1 issue)

Labels
Severity

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
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\Tests\Resources\DataFixtures\Phpcr;
15
16
use Doctrine\Common\DataFixtures\FixtureInterface;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use PHPCR\Util\NodeHelper;
19
use Sonata\DoctrinePHPCRAdminBundle\Tests\Resources\Document\Content;
20
21
class LoadTreeData implements FixtureInterface
22
{
23
    public function load(ObjectManager $manager): void
24
    {
25
        NodeHelper::createPath($manager->getPhpcrSession(), '/test');
26
27
        NodeHelper::createPath($manager->getPhpcrSession(), '/test/content');
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getPhpcrSession() does only exist in the following implementations of said interface: Doctrine\ODM\PHPCR\Decor...ocumentManagerDecorator, Doctrine\ODM\PHPCR\DocumentManager, Doctrine\Tests\ODM\PHPCR...ManagerGetClassMetadata.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
28
        NodeHelper::createPath($manager->getPhpcrSession(), '/test/routes');
29
30
        $contentRoot = $manager->find(null, '/test/content');
31
        $routeRoot = $manager->find(null, '/test/routes');
32
33
        $singleRoute = new Content();
34
        $singleRoute->setName('route-1');
35
        $singleRoute->setTitle('Route 1');
36
        $singleRoute->setParentDocument($routeRoot);
37
        $manager->persist($singleRoute);
38
39
        $routeAlikeA = new Content();
40
        $routeAlikeA->setName('route-2');
41
        $routeAlikeA->setTitle('Route 2');
42
        $routeAlikeA->setParentDocument($routeRoot);
43
        $manager->persist($routeAlikeA);
44
45
        $routeAlikeB = new Content();
46
        $routeAlikeB->setName('route-3');
47
        $routeAlikeB->setTitle('Route 3');
48
        $routeAlikeB->setParentDocument($routeRoot);
49
        $manager->persist($routeAlikeB);
50
51
        $child = new Content();
52
        $child->setName('child');
53
        $child->setTitle('Content Child');
54
55
        $content = new Content();
56
        $content->setName('content-1');
57
        $content->setTitle('Content 1');
58
        $content->setSingleRoute($singleRoute);
59
        $content->addRoute($routeAlikeA);
60
        $content->addRoute($routeAlikeB);
61
        $content->setChild($child);
62
        $content->setParentDocument($contentRoot);
63
        $manager->persist($content);
64
65
        $childA = new Content();
66
        $childA->setName('content-3');
67
        $childA->setTitle('Content Child A');
68
69
        $childB = new Content();
70
        $childB->setName('content-3');
71
        $childB->setTitle('Content Child B');
72
73
        $content = new Content();
74
        $content->setName('content-2');
75
        $content->setParentDocument($contentRoot);
76
        $content->addChild($childA);
77
        $content->addChild($childB);
78
        $content->setTitle('Content 2');
79
        $manager->persist($content);
80
81
        $manager->flush();
82
    }
83
}
84