Passed
Push — master ( a6c6f1...1f04b7 )
by
unknown
01:10 queued 10s
created

Migration20200915025000   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 52
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A description() 4 4 1
A setDependencies() 6 6 1
A up() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Charcoal\Email;
4
5
use Charcoal\DatabaseMigrator\AbstractMigration;
6
use Charcoal\Model\ModelFactoryTrait;
7
use PDOException;
8
use Pimple\Container;
9
10
/**
11
 * Migration 2020-09-15 02:25:00
12
 */
13 View Code Duplication
final class Migration20200915025000 extends AbstractMigration
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    use ModelFactoryTrait;
16
17
    /**
18
     * Short description of what the patch will do.
19
     *
20
     * @return string
21
     */
22
    public function description(): string
23
    {
24
        return 'Email queue item // add status property';
25
    }
26
27
    /**
28
     * Inject dependencies from a DI Container.
29
     *
30
     * @param Container $container A Pimple DI service container.
31
     * @return void
32
     */
33
    protected function setDependencies(Container $container): void
34
    {
35
        parent::setDependencies($container);
36
37
        $this->setModelFactory($container['model/factory']);
38
    }
39
40
    /**
41
     * Apply migration
42
     *
43
     * @return void
44
     */
45
    public function up(): void
46
    {
47
        try {
48
            /** @var EmailQueueItem $proto */
49
            $proto = $this->modelFactory()->create(EmailQueueItem::class);
50
            $table = $proto->source()->table();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Charcoal\Source\SourceInterface as the method table() does only exist in the following implementations of said interface: Charcoal\Source\DatabaseSource.

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...
51
52
            $this->addFeedback(
53
                "Updating <white>${table} Table</white>."
54
            );
55
56
            $this->createOrAlter($proto);
57
        } catch (PDOException $exception) {
58
            $this->addError($exception->getMessage());
59
            $this->setStatus(self::FAILED_STATUS);
60
        }
61
62
        $this->setStatus(self::PROCESSED_STATUS);
63
    }
64
}
65