Completed
Push — master ( 57b1db...16daf9 )
by Mike
03:22
created

UpgradeChanges   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 48
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 4 1
A getOrder() 0 4 1
A isRelevantTo() 0 4 1
A getBody() 0 12 1
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Element\Section;
4
5
use Sugarcrm\UpgradeSpec\Data\DataAwareInterface;
6
use Sugarcrm\UpgradeSpec\Data\DataAwareTrait;
7
use Sugarcrm\UpgradeSpec\Element\ElementInterface;
8
use Sugarcrm\UpgradeSpec\Context\Upgrade;
9
use Sugarcrm\UpgradeSpec\Template\RendererAwareInterface;
10
use Sugarcrm\UpgradeSpec\Template\RendererAwareTrait;
11
use Symfony\Component\Finder\Finder;
12
13
class UpgradeChanges implements ElementInterface, RendererAwareInterface, DataAwareInterface
14
{
15
    use RendererAwareTrait, DataAwareTrait;
16
17
    /**
18
     * @return string
19
     */
20
    public function getTitle()
21
    {
22
        return 'Review upgrade changes and fix possible customization conflicts';
23
    }
24
25
    /**
26
     * @return int
27
     */
28
    public function getOrder()
29
    {
30
        return 3;
31
    }
32
33
    /**
34
     * @param Upgrade $context
35
     *
36
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
     */
38
    public function isRelevantTo(Upgrade $context)
39
    {
40
        return $context->getTargetPath();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $context->getTargetPath(); (string) is incompatible with the return type declared by the interface Sugarcrm\UpgradeSpec\Ele...Interface::isRelevantTo of type boolean.

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
    /**
44
     * @param Upgrade $context
45
     *
46
     * @return string
47
     */
48
    public function getBody(Upgrade $context)
49
    {
50
        $customizations = $this->dataManager->getPotentiallyBrokenCustomizations($context);
51
52
        return $this->renderer->render('upgrade_changes', [
53
            'upgrade_steps' => $this->dataManager->getUpgradeSteps($context),
54
            'upgrade_to' => $context->getTargetVersion(),
55
            'upgrade_source' => $context->getTargetPath(),
56
            'modified_files' => $customizations['modified_files'],
57
            'deleted_files' => $customizations['deleted_files'],
58
        ]);
59
    }
60
}
61