CheckProductionSettings   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 42
loc 42
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 16 16 1
A execute() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the Drest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Lee Davis
9
 * @copyright Copyright (c) Lee Davis <@leedavis81>
10
 * @link https://github.com/leedavis81/drest/blob/master/LICENSE
11
 * @license http://opensource.org/licenses/MIT The MIT X License (MIT)
12
 */
13
namespace Drest\Tools\Console\Command;
14
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console;
19
20
/**
21
 * Check Drest Production Setting
22
 */
23 View Code Duplication
class CheckProductionSettings extends Command
24
{
25
    /**
26
     * @see Console\Command\Command
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('config:production-ready')
32
            ->setDescription('Checks the settings used are suitable for a production environment.')
33
            ->setHelp(
34
                <<<EOT
35
36
Checks the settings used are suitable for a production environment.
37
Notifications are given for using debug mode, or using a bad cache implementation.
38
39
Example usage:
40
php drest-server.php config:production-ready
41
EOT
42
            );
43
    }
44
45
    /**
46
     * @see Console\Command\Command
47
     *
48
     * @param InputInterface $input
49
     * @param OutputInterface $output
50
     * @return void
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        /* @var $drm \Drest\Manager */
55
        $drm = $this->getHelper('drm')->getDrestManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getDrestManager() does only exist in the following implementations of said interface: Drest\Tools\Console\Helper\DrestManagerHelper.

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...
56
57
        try {
58
            $drm->getConfiguration()->ensureProductionSettings();
59
            $output->write('Production settings OK' . PHP_EOL);
60
        } catch (\Exception $e) {
61
            $output->write(PHP_EOL . $e->getMessage() . PHP_EOL);
62
        }
63
    }
64
}
65