Failed Conditions
Push — modify-scrutinizeryml ( 361e25...08b4c1 )
by Kentaro
63:54 queued 57:30
created

Acceptance::getBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Helper;
3
// here you can define custom actions
4
// all public methods declared in helper class will be available in $I
5
6
class Acceptance extends \Codeception\Module
7
{
8
    public function _initialize()
9
    {
10
        $this->clearDownloadDir();
11
    }
12
13
    private function clearDownloadDir()
14
    {
15
        $downloadDir = dirname(__DIR__).'/_downloads/';
16
        if (file_exists($downloadDir)) {
17
            $files = scandir($downloadDir);
18
            $files = array_filter($files, function ($fileName) use ($downloadDir) {
19
                return is_file($downloadDir.$fileName) && (strpos($fileName, '.') != 0);
20
            });
21
            foreach ($files as $f) {
22
                unlink($downloadDir.$f);
23
            }
24
        }
25
    }
26
27
    public function getBaseUrl()
28
    {
29
        return $this->getModule('WebDriver')->_getUrl();
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 _getUrl() does only exist in the following sub-classes of Codeception\Module: Codeception\Module\AngularJS, Codeception\Module\PhpBrowser, Codeception\Module\WebDriver. 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...
30
    }
31
}
32