BrowserTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 73
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadPage() 0 4 1
A getBrowserModule() 0 4 1
A getCurrentUriParam() 0 8 1
A getCurrentUri() 0 4 1
A getCurrentUrl() 0 4 1
A seeFullUrlEquals() 0 6 1
1
<?php
2
3
namespace ByTIC\Codeception\Helper\Traits;
4
5
use Codeception\Module\PhpBrowser;
6
7
/**
8
 * Class UriTrait
9
 * @package ByTIC\Common\Tests\Helper\Traits
10
 */
11
trait BrowserTrait
12
{
13
    use AbstractTrait;
14
15
    /**
16
     * @param $method
17
     * @param $url
18
     * @param $post
19
     *
20
     * @throws \Codeception\Exception\ModuleException
21
     */
22
    public function loadPage($method, $url, $post)
23
    {
24
        return $this->getBrowserModule()->_loadPage($method, $url, $post);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Codeception\Module as the method _loadPage() does only exist in the following sub-classes of Codeception\Module: Codeception\Lib\Framework, Codeception\Lib\InnerBrowser, Codeception\Module\PhpBrowser. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
25
    }
26
27
    /**
28
     * @return \Codeception\Module|PhpBrowser
29
     * @throws \Codeception\Exception\ModuleException
30
     */
31
    protected function getBrowserModule()
32
    {
33
        return $this->getModule('PhpBrowser');
34
    }
35
36
37
    /**
38
     * @param $name
39
     *
40
     * @return mixed
41
     * @throws \Codeception\Exception\ModuleException
42
     */
43
    public function getCurrentUriParam($name)
44
    {
45
        $uri   = $this->getCurrentUri();
46
        $query = parse_url($uri, PHP_URL_QUERY);
47
        parse_str($query, $params);
48
49
        return $params[$name];
50
    }
51
52
    /**
53
     * @return mixed
54
     * @throws \Codeception\Exception\ModuleException
55
     */
56
    public function getCurrentUri()
57
    {
58
        return $this->getBrowserModule()->_getCurrentUri();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Codeception\Module as the method _getCurrentUri() does only exist in the following sub-classes of Codeception\Module: Codeception\Lib\Framework, Codeception\Lib\InnerBrowser, Codeception\Module\PhpBrowser. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59
    }
60
61
62
    /**
63
     * @return mixed
64
     * @throws \Codeception\Exception\ModuleException
65
     */
66
    public function getCurrentUrl()
67
    {
68
        return $this->getBrowserModule()->client->getHistory()->current()->getUri();
0 ignored issues
show
Bug introduced by
The property client does not seem to exist in Codeception\Module.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
    }
70
71
72
    /**
73
     * @param $expected
74
     *
75
     * @throws \Codeception\Exception\ModuleException
76
     */
77
    public function seeFullUrlEquals($expected)
78
    {
79
        $url = $this->getCurrentUrl();
80
81
        $this->assertEquals($expected, $url);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
    }
83
}
84