Completed
Push — master ( eeedf4...3b415d )
by Alejandro
02:34
created

MailServiceAwareTrait::setMailService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace AcMailer\Service;
3
4
use Zend\ServiceManager\ServiceLocatorAwareInterface;
5
6
/**
7
 * Class MailServiceAwareTrait
8
 * @author Alejandro Celaya Alastrué
9
 * @link http://www.alejandrocelaya.com
10
 */
11
trait MailServiceAwareTrait
12
{
13
    /**
14
     * @var MailServiceInterface
15
     */
16
    protected $mailService;
17
18
    /**
19
     * @param MailServiceInterface $mailService
20
     * @return $this
21
     */
22
    public function setMailService(MailServiceInterface $mailService)
23
    {
24
        $this->mailService = $mailService;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @return MailServiceInterface
31
     */
32
    public function getMailService()
33
    {
34
        if (
35
            $this instanceof ServiceLocatorAwareInterface
36
            && ! isset($this->mailService)
0 ignored issues
show
Bug introduced by
Accessing mailService on the interface Zend\ServiceManager\ServiceLocatorAwareInterface 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
            && $this->getServiceLocator()->has('AcMailer\Service\MailService')
38
        ) {
39
            $this->mailService = $this->getServiceLocator()->get('AcMailer\Service\MailService');
0 ignored issues
show
Bug introduced by
Accessing mailService on the interface Zend\ServiceManager\ServiceLocatorAwareInterface 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...
40
        }
41
42
        return $this->mailService;
43
    }
44
}
45