Completed
Push — master ( 2fdc96...4f1f24 )
by Damian
12:09
created

CleanuPTestDatabasesTask::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Cleans up leftover databases from aborted test executions (starting with ss_tmpdb)
4
 * Task is restricted to users with administrator rights or running through CLI.
5
 *
6
 * @package framework
7
 * @subpackage tasks
8
 */
9
class CleanuPTestDatabasesTask extends BuildTask {
10
	protected $title = 'Deletes all temporary test databases';
11
12
	protected $description = 'Cleans up leftover databases from aborted test executions (starting with ss_tmpdb)';
13
14
	public function init() {
15
		parent::init();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BuildTask as the method init() does only exist in the following sub-classes of BuildTask: CleanuPTestDatabasesTask, EncryptAllPasswordsTask, RegenerateCachedImagesTask, i18nTextCollectorTask. 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...
16
17
		if(!Permission::check('ADMIN') && !Director::is_cli()) {
18
			return Security::permissionFailure($this);
19
		}
20
	}
21
22
	public function run() {
23
		SapphireTest::delete_all_temp_dbs();
24
	}
25
26
}