Completed
Pull Request — master (#250)
by
unknown
20:15
created

CpuPerformance::check()   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
declare(strict_types=1);
4
5
namespace Liip\MonitorBundle\Check;
6
7
use Laminas\Diagnostics\Check\CheckInterface;
8
use Laminas\Diagnostics\Check\CpuPerformance as LaminasCpuPerformance;
9
use Laminas\Diagnostics\Result\ResultInterface;
10
11
class CpuPerformance implements CheckInterface
12
{
13
    /**
14
     * @var CheckInterface
15
     */
16
    private $check;
17
18
    public function __construct(array $config = [])
19
    {
20
        $minPerformance = $config['performance'];
21
22
        $check = new LaminasCpuPerformance($minPerformance);
23
24
        $label = $config['label'] ?? sprintf('Performance is `%s`', $minPerformance);
25
        $check->setLabel($label);
26
27
        $this->check = $check;
28
    }
29
30
    public function check(): ResultInterface
31
    {
32
        return $this->check->check();
33
    }
34
35
    public function getLabel(): string
36
    {
37
        return $this->check->getLabel();
38
    }
39
40
    public function setLabel(string $label): void
41
    {
42
        $this->check->setLabel($label);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Laminas\Diagnostics\Check\CheckInterface as the method setLabel() does only exist in the following implementations of said interface: Laminas\Diagnostics\Check\AbstractCheck, Laminas\Diagnostics\Check\AbstractFileCheck, Laminas\Diagnostics\Check\AbstractMemoryCheck, Laminas\Diagnostics\Check\ApcFragmentation, Laminas\Diagnostics\Check\ApcMemory, Laminas\Diagnostics\Check\Callback, Laminas\Diagnostics\Check\ClassExists, Laminas\Diagnostics\Check\CouchDBCheck, Laminas\Diagnostics\Check\CpuPerformance, Laminas\Diagnostics\Check\DirReadable, Laminas\Diagnostics\Check\DirWritable, Laminas\Diagnostics\Check\DiskFree, Laminas\Diagnostics\Check\DiskUsage, Laminas\Diagnostics\Check\DoctrineMigration, Laminas\Diagnostics\Check\ExtensionLoaded, Laminas\Diagnostics\Check\GuzzleHttpService, Laminas\Diagnostics\Check\HttpService, Laminas\Diagnostics\Check\IniFile, Laminas\Diagnostics\Check\JsonFile, Laminas\Diagnostics\Check\Memcache, Laminas\Diagnostics\Check\Memcached, Laminas\Diagnostics\Check\Mongo, Laminas\Diagnostics\Check\OpCacheMemory, Laminas\Diagnostics\Check\PhpFlag, Laminas\Diagnostics\Check\PhpVersion, Laminas\Diagnostics\Check\ProcessRunning, Laminas\Diagnostics\Check\RabbitMQ, Laminas\Diagnostics\Check\Redis, Laminas\Diagnostics\Check\SecurityAdvisory, Laminas\Diagnostics\Check\StreamWrapperExists, Laminas\Diagnostics\Check\XmlFile, Laminas\Diagnostics\Check\YamlFile, Liip\MonitorBundle\Check\ApcFragmentation, Liip\MonitorBundle\Check\ApcMemory, Liip\MonitorBundle\Check\ClassExists, Liip\MonitorBundle\Check\CpuPerformance, Liip\MonitorBundle\Check\DiskUsage, Liip\MonitorBundle\Check\DoctrineDbal, Liip\MonitorBundle\Check\DoctrineMongoDb, Liip\MonitorBundle\Check\IniFile, Liip\MonitorBundle\Check\JsonFile, Liip\MonitorBundle\Check\OpCacheMemory, Liip\MonitorBundle\Check\PhpExtension, Liip\MonitorBundle\Check\ReadableDirectory, Liip\MonitorBundle\Check\SecurityAdvisory, Liip\MonitorBundle\Check\StreamWrapperExists, Liip\MonitorBundle\Check\WritableDirectory, Liip\MonitorBundle\Check\XmlFile, Liip\MonitorBundle\Check\YamlFile.

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...
43
    }
44
}
45