LoadDocumentBench::benchLoadReferenceOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Benchmark\Document;
6
7
use DateTimeImmutable;
8
use Doctrine\ODM\MongoDB\Benchmark\BaseBench;
9
use Documents\Account;
10
use Documents\Address;
11
use Documents\Group;
12
use Documents\Phonenumber;
13
use Documents\User;
14
use MongoDB\BSON\ObjectId;
15
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
16
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
17
use function assert;
18
19
/**
20
 * @BeforeMethods({"init"}, extend=true)
21
 */
22
final class LoadDocumentBench extends BaseBench
23
{
24
    /** @var ObjectId */
25
    private static $userId;
26
27
    public function init()
28
    {
29
        self::$userId = new ObjectId();
30
31
        $account = new Account();
0 ignored issues
show
Bug introduced by
The call to Account::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
32
        $account->setName('alcaeus');
33
34
        $address = new Address();
35
        $address->setAddress('Redacted');
0 ignored issues
show
Bug introduced by
The method setAddress() does not seem to exist on object<Documents\Address>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        $address->setCity('Munich');
37
38
        $group1 = new Group('One');
39
        $group2 = new Group('Two');
40
41
        $user = new User();
42
        $user->setId(self::$userId);
0 ignored issues
show
Bug introduced by
The method setId() does not seem to exist on object<Documents\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        $user->setUsername('alcaeus');
44
        $user->setCreatedAt(new DateTimeImmutable());
0 ignored issues
show
Bug introduced by
The method setCreatedAt() does not seem to exist on object<Documents\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $user->setAddress($address);
46
        $user->setAccount($account);
47
        $user->addPhonenumber(new Phonenumber('12345678'));
48
        $user->addGroup($group1);
0 ignored issues
show
Bug introduced by
The method addGroup() does not seem to exist on object<Documents\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        $user->addGroup($group2);
0 ignored issues
show
Bug introduced by
The method addGroup() does not seem to exist on object<Documents\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
51
        $this->getDocumentManager()->persist($user);
52
        $this->getDocumentManager()->flush();
53
54
        $this->getDocumentManager()->clear();
55
    }
56
57
    /**
58
     * @Warmup(2)
59
     */
60
    public function benchLoadDocument()
61
    {
62
        $this->loadDocument();
63
    }
64
65
    /**
66
     * @Warmup(2)
67
     */
68
    public function benchLoadEmbedOne()
69
    {
70
        $this->loadDocument()->getAddress()->getCity();
0 ignored issues
show
Unused Code introduced by
The call to the method Documents\Address::getCity() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
71
    }
72
73
    /**
74
     * @Warmup(2)
75
     */
76
    public function benchLoadEmbedMany()
77
    {
78
        $this->loadDocument()->getPhonenumbers()->forAll(static function ($key, Phonenumber $element) {
79
            return $element->getPhoneNumber();
80
        });
81
    }
82
83
    /**
84
     * @Warmup(2)
85
     */
86
    public function benchLoadReferenceOne()
87
    {
88
        $this->loadDocument()->getAccount()->getName();
0 ignored issues
show
Unused Code introduced by
The call to the method Documents\Account::getName() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
89
    }
90
91
    /**
92
     * @Warmup(2)
93
     */
94
    public function benchLoadReferenceMany()
95
    {
96
        $this->loadDocument()->getGroups()->forAll(static function ($key, Group $group) {
0 ignored issues
show
Bug introduced by
The method getGroups() does not seem to exist on object<Documents\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            return $group->getName();
98
        });
99
    }
100
101
    /**
102
     * @return User
103
     */
104
    private function loadDocument()
105
    {
106
        $document = $this->getDocumentManager()->find(User::class, self::$userId);
107
        assert($document instanceof User);
108
109
        return $document;
110
    }
111
}
112