Completed
Push — master ( d3a073...5737c8 )
by Greg
02:21
created

tests/_helpers/CliHelper.php (2 issues)

strict.coding_against_specific_subtype

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Codeception\Module;
3
4
use Robo\Robo;
5
use Robo\Collection\CollectionBuilder;
6
use Robo\Task\ValueProviderTask;
7
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
use League\Container\ContainerAwareInterface;
11
use League\Container\ContainerAwareTrait;
12
13
class CliHelper extends \Codeception\Module implements ContainerAwareInterface
14
{
15
    use ContainerAwareTrait;
16
    use SeeInOutputTrait;
17
18
    use \Robo\LoadAllTasks {
19
        task as public;
20
        taskExec as public;
21
        taskExecStack as public;
22
        taskWriteToFile as public;
23
        taskReplaceInFile as public;
24
        taskConcat as public;
25
        taskTmpFile as public;
26
        taskCleanDir as public;
27
        taskCopyDir as public;
28
        taskGenTask as public;
29
        taskDeleteDir as public;
30
        taskFlattenDir as public;
31
        taskFilesystemStack as public;
32
        taskForEach as public;
33
        taskTmpDir as public;
34
        taskMinify as public;
35
        _copyDir as public shortcutCopyDir;
36
        _mirrorDir as public shortcutMirrorDir;
37
        _tmpDir as public shortcutTmpDir;
38
        taskPack as public;
39
        taskExtract as public;
40
        setBuilder as public;
41
    }
42
43
    public function collectionBuilder()
44
    {
45
        $tasks = new CliHelperTasks();
46
        $builder = CollectionBuilder::create($this->getContainer(), $tasks);
47
        $tasks->setBuilder($builder);
48
49
        return $builder;
50
    }
51
52
    public function seeDirFound($dir)
53
    {
54
        $this->assertTrue(is_dir($dir) && file_exists($dir), "Directory does not exist");
55
    }
56
57
    public function _before(\Codeception\TestCase $test) {
58
        $container = new \League\Container\Container();
59
        $this->initSeeInOutputTrait($container);
60
        Robo::setContainer($container);
61
        $this->setContainer($container);
62
63
        $this->getModule('Filesystem')->copyDir(codecept_data_dir().'claypit', codecept_data_dir().'sandbox');
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Codeception\Module as the method copyDir() does only exist in the following sub-classes of Codeception\Module: Codeception\Module\FTP, Codeception\Module\Filesystem. 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...
64
    }
65
66
    public function _after(\Codeception\TestCase $test) {
67
        $this->getModule('Filesystem')->deleteDir(codecept_data_dir().'sandbox');
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Codeception\Module as the method deleteDir() does only exist in the following sub-classes of Codeception\Module: Codeception\Module\FTP, Codeception\Module\Filesystem. 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...
68
        $this->getContainer()->add('output', new ConsoleOutput());
69
        chdir(codecept_root_dir());
70
    }
71
}
72