Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
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
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
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 Helper;
15
16
// here you can define custom actions
17
// all public methods declared in helper class will be available in $I
18
19
class Acceptance extends \Codeception\Module
20
{
21
    public function _initialize()
22
    {
23
        $this->clearDownloadDir();
24
    }
25
26
    private function clearDownloadDir()
27
    {
28
        $downloadDir = dirname(__DIR__).'/_downloads/';
29
        if (file_exists($downloadDir)) {
30
            $files = scandir($downloadDir);
31
            $files = array_filter($files, function ($fileName) use ($downloadDir) {
32
                return is_file($downloadDir.$fileName) && (strpos($fileName, '.') != 0);
33
            });
34
            foreach ($files as $f) {
35
                unlink($downloadDir.$f);
36
            }
37
        }
38
    }
39
40
    public function getBaseUrl()
41
    {
42
        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...
43
    }
44
}
45