CloneStrategy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 62
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B deploy() 0 27 5
A update() 0 12 2
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\CreateRelease;
14
15
use Rocketeer\Strategies\AbstractStrategy;
16
17
/**
18
 * Clones a fresh instance of the repository by VCS.
19
 */
20
class CloneStrategy extends AbstractStrategy implements CreateReleaseStrategyInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $description = 'Clones a fresh instance of the repository by VCS';
26
27
    /**
28
     * Deploy a new clean copy of the application.
29
     *
30
     * @param string|null $destination
31
     *
32
     * @return bool
33
     */
34 20
    public function deploy($destination = null)
35
    {
36 20
        if (!$destination) {
37 19
            $destination = $this->releasesManager->getCurrentReleasePath();
38 19
        }
39
40
        // Executing checkout
41 20
        $this->explainer->line('Cloning repository in "'.$destination.'"');
42 20
        $output = $this->vcs->run('checkout', $destination);
0 ignored issues
show
Bug introduced by Maxime Fabre
The method run() does not seem to exist on object<Rocketeer\Binaries\Vcs\VcsInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        // Cancel if failed and forget credentials
45 20
        $success = $this->bash->displayStatusMessage('Unable to clone the repository', $output) !== false;
46 20
        if (!$success) {
47 1
            $this->localStorage->forget('credentials');
48
49 1
            return false;
50
        }
51
52
        // Deploy submodules
53 19
        if ($this->config->getContextually('vcs.submodules') && $this->vcs->submodules()) {
0 ignored issues
show
Bug introduced by Maxime Fabre
It seems like you code against a concrete implementation and not the interface Rocketeer\Services\Config\ConfigurationInterface as the method getContextually() does only exist in the following implementations of said interface: Rocketeer\Services\Config\ContextualConfiguration.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54 11
            $this->explainer->line('Initializing submodules if any');
55 11
            $this->vcs->runForCurrentRelease('submodules');
0 ignored issues
show
Bug introduced by Maxime Fabre
The method runForCurrentRelease() does not seem to exist on object<Rocketeer\Binaries\Vcs\VcsInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56 11
            $success = $this->status();
57 11
        }
58
59 19
        return $success;
60
    }
61
62
    /**
63
     * Update the latest version of the application.
64
     *
65
     * @param bool $reset
66
     *
67
     * @return string
68
     */
69 6
    public function update($reset = true)
70
    {
71 6
        $this->explainer->info('Pulling changes');
72 6
        $tasks = [$this->vcs->update()];
73
74
        // Reset if requested
75 6
        if ($reset) {
76 6
            array_unshift($tasks, $this->vcs->reset());
77 6
        }
78
79 6
        return $this->bash->runForCurrentRelease($tasks);
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method runForCurrentRelease does not exist on object<Rocketeer\Services\Connections\Shell\Bash>? 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...
80
    }
81
}
82