UploadStrategy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 56
ccs 0
cts 17
cp 0
rs 10
c 1
b 1
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B onReleaseReady() 0 42 4
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Strategies\Deploy;
14
15
use Symfony\Component\Finder\Finder;
16
17
/**
18
 * Deploys locally and uploads the files through FTP.
19
 */
20
class UploadStrategy extends AbstractLocalDeployStrategy
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $description = 'Deploys locally and uploads the files through FTP';
26
27
    /**
28
     * @param string $from
29
     * @param string $to
30
     *
31
     * @return mixed
32
     */
33
    protected function onReleaseReady($from, $to)
0 ignored issues
show
Comprehensibility introduced by Maxime Fabre
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
34
    {
35
        $this->explainer->comment(sprintf('Uploading files from %s to %s', $from, $to));
36
37
        // Only gather files from existing folders (for pretend mode)
38
        $folders = array_filter([$from], 'is_dir');
39
        if (!$folders) {
40
            return true;
0 ignored issues
show
Bug Best Practice introduced by Maxime Fabre
The return type of return true; (boolean) is incompatible with the return type declared by the abstract method Rocketeer\Strategies\Dep...trategy::onReleaseReady of type null|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
        }
42
43
        $files = (new Finder())->in($folders)->exclude(['.git']);
44
        $format = $this->explainer->getProgressBarFormat(
45
            '%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%'.PHP_EOL.
46
            '%message%'
47
        );
48
49
        // Create progress bar
50
        $progress = $this->command->createProgressBar(iterator_count($files));
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method createProgressBar does not exist on object<Rocketeer\Console...mmands\AbstractCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
        $progress->setFormat($format);
52
        $progress->start();
53
54
        foreach ($files as $file) {
55
            $origin = str_replace($from, null, $file->getPathname());
56
            $path = $to.DS.str_replace($from, null, $file->getPathname());
57
            $progress->setMessage(
58
                dirname($origin).DS.
59
                '<info>'.basename($origin).'</info>'
60
            );
61
62
            if ($file->isDir()) {
63
                $this->createFolder($path);
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method createFolder does not exist on object<Rocketeer\Strateg...\Deploy\UploadStrategy>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64
            } else {
65
                $this->put($path, $file->getContents());
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method put does not exist on object<Rocketeer\Strateg...\Deploy\UploadStrategy>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
            }
67
68
            $progress->advance();
69
        }
70
71
        $progress->finish();
72
73
        return true;
0 ignored issues
show
Bug Best Practice introduced by Maxime Fabre
The return type of return true; (boolean) is incompatible with the return type declared by the abstract method Rocketeer\Strategies\Dep...trategy::onReleaseReady of type null|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74
    }
75
}
76