Passed
Push — master ( 70e3a4...343a5d )
by Timothy
02:04
created

MonologServiceAwareTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setMonologService() 0 4 1
B getMonologService() 0 17 6
1
<?php
2
/**
3
 * @author Evgeny Shpilevsky <[email protected]>
4
 */
5
6
namespace EnliteMonolog\Service;
7
8
use Monolog\Logger;
9
use RuntimeException;
10
use Zend\ServiceManager\ServiceLocatorAwareInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
trait MonologServiceAwareTrait
14
{
15
16
    /**
17
     * @var Logger
18
     */
19
    protected $monologService;
20
21
    /**
22
     * @var string
23
     */
24
    protected $monologLoggerName = 'EnliteMonologService';
25
26
    /**
27
     * @param Logger $monologService
28
     */
29 1
    public function setMonologService(Logger $monologService)
30
    {
31 1
        $this->monologService = $monologService;
32 1
    }
33
34
    /**
35
     * @throws \RuntimeException
36
     * @return Logger
37
     */
38 4
    public function getMonologService()
39
    {
40 4
        if (null === $this->monologService) {
41 3
            if ($this instanceof ServiceLocatorAwareInterface || method_exists($this, 'getServiceLocator')) {
0 ignored issues
show
Bug introduced by
The class Zend\ServiceManager\ServiceLocatorAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
42 1
                $this->monologService = $this->getServiceLocator()->get($this->monologLoggerName);
0 ignored issues
show
Bug introduced by
It seems like getServiceLocator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
            } else {
44 2
                if (property_exists($this, 'serviceLocator')
45 2
                    && $this->serviceLocator instanceof ServiceLocatorInterface
46
                ) {
47 1
                    $this->monologService = $this->serviceLocator->get($this->monologLoggerName);
0 ignored issues
show
Bug introduced by
The property serviceLocator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
                } else {
49 1
                    throw new RuntimeException('Service locator not found');
50
                }
51
            }
52
        }
53 3
        return $this->monologService;
54
    }
55
}
56