Completed
Push — master ( 7c230f...19e916 )
by
unknown
02:26
created

Migration20200827131900::description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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-08-27 13:19:00
12
 */
13
class Migration20200827131900 extends AbstractMigration
14
{
15
    use ModelFactoryTrait;
16
17
    /**
18
     * @return string
19
     */
20
    public function description(): string
21
    {
22
        return 'Email queue item // add expiry date';
23
    }
24
25
    /**
26
     * Inject dependencies from a DI Container.
27
     *
28
     * @param Container $container A Pimple DI service container.
29
     * @return void
30
     */
31
    protected function setDependencies(Container $container): void
32
    {
33
        parent::setDependencies($container);
34
35
        $this->setModelFactory($container['model/factory']);
36
    }
37
38
    /**
39
     * Apply migration
40
     *
41
     * @return void
42
     */
43
    public function up(): void
44
    {
45
        try {
46
            /** @var EmailQueueItem $proto */
47
            $proto = $this->modelFactory()->create(EmailQueueItem::class);
48
            $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...
49
50
            $this->addFeedback(
51
                "Updating <white>${table} Table</white>."
52
            );
53
54
            $this->createOrAlter($proto);
55
        } catch (PDOException $exception) {
56
            $this->addError($exception->getMessage());
57
            $this->setStatus(self::FAILED_STATUS);
58
        }
59
60
        $this->setStatus(self::PROCESSED_STATUS);
61
    }
62
63
}
64