Completed
Push — master ( 7c3e82...3ac6f4 )
by Andreas
11:47 queued 11s
created

LoadDocumentBench::benchLoadReferenceOne()   A

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
18
/**
19
 * @BeforeMethods({"init"}, extend=true)
20
 */
21
final class LoadDocumentBench extends BaseBench
22
{
23
    /** @var ObjectId */
24
    private static $userId;
25
26
    public function init()
27
    {
28
        self::$userId = new ObjectId();
29
30
        $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...
31
        $account->setName('alcaeus');
32
33
        $address = new Address();
34
        $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...
35
        $address->setCity('Munich');
36
37
        $group1 = new Group('One');
38
        $group2 = new Group('Two');
39
40
        $user = new User();
41
        $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...
42
        $user->setUsername('alcaeus');
43
        $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...
44
        $user->setAddress($address);
45
        $user->setAccount($account);
46
        $user->addPhonenumber(new Phonenumber('12345678'));
47
        $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...
48
        $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...
49
50
        $this->getDocumentManager()->persist($user);
51
        $this->getDocumentManager()->flush();
52
53
        $this->getDocumentManager()->clear();
54
    }
55
56
    /**
57
     * @Warmup(2)
58
     */
59
    public function benchLoadDocument()
60
    {
61
        $this->loadDocument();
62
    }
63
64
    /**
65
     * @Warmup(2)
66
     */
67
    public function benchLoadEmbedOne()
68
    {
69
        $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...
70
    }
71
72
    /**
73
     * @Warmup(2)
74
     */
75
    public function benchLoadEmbedMany()
76
    {
77
        $this->loadDocument()->getPhonenumbers()->forAll(static function ($key, Phonenumber $element) {
78
            return $element->getPhoneNumber();
79
        });
80
    }
81
82
    /**
83
     * @Warmup(2)
84
     */
85
    public function benchLoadReferenceOne()
86
    {
87
        $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...
88
    }
89
90
    /**
91
     * @Warmup(2)
92
     */
93
    public function benchLoadReferenceMany()
94
    {
95
        $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...
96
            return $group->getName();
97
        });
98
    }
99
100
    /**
101
     * @return User
102
     */
103
    private function loadDocument()
104
    {
105
        $document = $this->getDocumentManager()->find(User::class, self::$userId);
106
        assert($document instanceof User);
107
108
        return $document;
109
    }
110
}
111