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

MailServiceAwareTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setMailService() 0 6 1
A getMailService() 0 12 4
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