PhpCodeSnifferReviewNoBlade::review()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 8.439
cc 6
eloc 16
nc 6
nop 2
1
<?php
2
3
/*
4
 * This file is part of StaticReview
5
 *
6
 * Copyright (c) 2014 Samuel Parkinson <@samparkinson_>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @see http://github.com/sjparkinson/static-review/blob/master/LICENSE
12
 */
13
14
namespace Padosoft\StaticReview\PHP;
15
16
use StaticReview\File\FileInterface;
17
use StaticReview\Reporter\ReporterInterface;
18
use StaticReview\Review\AbstractFileReview;
19
use StaticReview\Review\ReviewableInterface;
20
21
class PhpCodeSnifferReviewNoBlade extends AbstractFileReview
22
{
23
    protected $options = [];
24
25
    /**
26
     * Gets the value of an option.
27
     *
28
     * @param  string $option
29
     * @return string
30
     */
31
    public function getOption($option)
32
    {
33
        return $this->options[$option];
34
    }
35
36
    /**
37
     * Gets a string of the set options to pass to the command line.
38
     *
39
     * @return string
40
     */
41
    public function getOptionsForConsole()
42
    {
43
        $builder = '';
44
45
        foreach ($this->options as $option => $value) {
46
            $builder .= '--' . $option;
47
48
            if ($value) {
49
                $builder .= '=' . $value;
50
            }
51
52
            $builder .= ' ';
53
        }
54
55
        return $builder;
56
    }
57
58
    /**
59
     * Adds an option to be included when running PHP_CodeSniffer. Overwrites the values of options with the same name.
60
     *
61
     * @param  string               $option
62
     * @param  string               $value
63
     * @return PhpCodeSnifferReview
64
     */
65
    public function setOption($option, $value)
66
    {
67
        if ($option === 'report') {
68
            throw new \RuntimeException('"report" is not a valid option name.');
69
        }
70
71
        $this->options[$option] = $value;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Determins if a given file should be reviewed.
78
     *
79
     * @param  FileInterface $file
80
     * @return bool
81
     */
82
    public function canReviewFile(FileInterface $file)
83
    {
84
        return ($file->getExtension() === 'php' && substr($file->getFileName(),-strlen('blade.php'))!='blade.php');
85
    }
86
87
    /**
88
     * Checks PHP files using PHP_CodeSniffer.
89
     */
90
    public function review(ReporterInterface $reporter, ReviewableInterface $file)
91
    {
92
        $bin = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, 'vendor/bin/phpcs');
93
        $cmd = $bin . ' --report=json ';
94
95
        if ($this->getOptionsForConsole()) {
96
            $cmd .= $this->getOptionsForConsole();
97
        }
98
99
        $cmd .= $file->getFullPath();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface StaticReview\Review\ReviewableInterface as the method getFullPath() does only exist in the following implementations of said interface: StaticReview\File\File.

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...
100
101
        $process = $this->getProcess($cmd);
102
        $process->run();
103
104
        if (! $process->isSuccessful()) {
105
            // Create the array of outputs and remove empty values.
106
            $output = json_decode($process->getOutput(), true);
107
108
            $filter = function ($acc, $file) {
109
                if ($file['errors'] > 0 || $file['warnings'] > 0) {
110
                    return $acc + $file['messages'];
111
                }
112
            };
113
114
            foreach (array_reduce($output['files'], $filter, []) as $error) {
115
                $message = $error['message'] . ' on line ' . $error['line'];
116
                $reporter->warning($message, $this, $file);
117
            }
118
        }
119
    }
120
}
121