DefaultEmailChangeStrategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Strategy;
13
14
use Da\User\Contracts\MailChangeStrategyInterface;
15
use Da\User\Factory\MailFactory;
16
use Da\User\Factory\TokenFactory;
17
use Da\User\Form\SettingsForm;
18
use Da\User\Traits\ContainerAwareTrait;
19
use Yii;
20
21
class DefaultEmailChangeStrategy implements MailChangeStrategyInterface
22
{
23
    use ContainerAwareTrait;
24
25
    protected $form;
26
27 1
    public function __construct(SettingsForm $form)
28
    {
29 1
        $this->form = $form;
30 1
    }
31
32 1
    public function run()
33
    {
34 1
        $this->form->getUser()->unconfirmed_email = $this->form->email;
0 ignored issues
show
Bug introduced by
Accessing unconfirmed_email on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
35
36 1
        $token = TokenFactory::makeConfirmNewMailToken($this->form->getUser()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
37
38 1
        $mailService = MailFactory::makeReconfirmationMailerService($this->form->getUser(), $token);
0 ignored issues
show
Compatibility introduced by
$this->form->getUser() of type object<yii\web\IdentityInterface> is not a sub-type of object<Da\User\Model\User>. It seems like you assume a concrete implementation of the interface yii\web\IdentityInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
39
40 1
        if ($mailService->run()) {
41 1
            Yii::$app
42 1
                ->session
43 1
                ->setFlash('info', Yii::t('usuario', 'A confirmation message has been sent to your new email address'));
44
45 1
            return $this->form->getUser()->save();
46
        }
47
48
        return false;
49
    }
50
}
51