Completed
Push — 2.x-dev-kit ( 97223a...27f7bf )
by Grégoire
12:08
created

LoadTreeData::load()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0
cc 1
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Fixtures\App\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\Fixtures\App\Document\Content;
20
21
class LoadTreeData implements FixtureInterface
22
{
23
    public function load(ObjectManager $manager): void
24
    {
25
        NodeHelper::createPath($manager->getPhpcrSession(), '/test');
0 ignored issues
show
Bug introduced by
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...
26
27
        NodeHelper::createPath($manager->getPhpcrSession(), '/test/content');
0 ignored issues
show
Bug introduced by
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');
0 ignored issues
show
Bug introduced by
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...
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);
0 ignored issues
show
Bug introduced by
It seems like $routeRoot defined by $manager->find(null, '/test/routes') on line 31 can also be of type null; however, Sonata\DoctrinePHPCRAdmi...nt::setParentDocument() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
        $manager->persist($singleRoute);
38
39
        $routeAlikeA = new Content();
40
        $routeAlikeA->setName('route-2');
41
        $routeAlikeA->setTitle('Route 2');
42
        $routeAlikeA->setParentDocument($routeRoot);
0 ignored issues
show
Bug introduced by
It seems like $routeRoot defined by $manager->find(null, '/test/routes') on line 31 can also be of type null; however, Sonata\DoctrinePHPCRAdmi...nt::setParentDocument() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
        $manager->persist($routeAlikeA);
44
45
        $routeAlikeB = new Content();
46
        $routeAlikeB->setName('route-3');
47
        $routeAlikeB->setTitle('Route 3');
48
        $routeAlikeB->setParentDocument($routeRoot);
0 ignored issues
show
Bug introduced by
It seems like $routeRoot defined by $manager->find(null, '/test/routes') on line 31 can also be of type null; however, Sonata\DoctrinePHPCRAdmi...nt::setParentDocument() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $contentRoot defined by $manager->find(null, '/test/content') on line 30 can also be of type null; however, Sonata\DoctrinePHPCRAdmi...nt::setParentDocument() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $contentRoot defined by $manager->find(null, '/test/content') on line 30 can also be of type null; however, Sonata\DoctrinePHPCRAdmi...nt::setParentDocument() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
        $content->addChild($childA);
77
        $content->addChild($childB);
78
        $content->setTitle('Content 2');
79
        $manager->persist($content);
80
81
        $manager->flush();
82
    }
83
}
84