PhpLeadingLineReviewNoBlade::canReviewFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
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 PhpLeadingLineReviewNoBlade extends AbstractFileReview
22
{
23
    /**
24
     * Determins if the given file should be revewed.
25
     *
26
     * @param  FileInterface $file
27
     * @return bool
28
     */
29
    public function canReviewFile(FileInterface $file)
30
    {
31
        return ($file->getExtension() === 'php' && substr($file->getFileName(),-strlen('blade.php'))!='blade.php');
32
    }
33
34
    /**
35
     * Checks if the set file starts with the correct character sequence, which
36
     * helps to stop any rouge whitespace making it in before the first php tag.
37
     *
38
     * @link http://stackoverflow.com/a/2440685
39
     */
40
    public function review(ReporterInterface $reporter, ReviewableInterface $file)
41
    {
42
43
        $f = fopen($file->getFullPath(), 'r');
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...
44
        $line = fgets($f);
45
        fclose($f);
46
47
48
        if (! in_array(trim($line), ['<?php', '#!/usr/bin/env php'])) {
49
            $message = 'File must begin with `<?php` or `#!/usr/bin/env php`';
50
            $reporter->error($message, $this, $file);
51
52
        }
53
    }
54
}
55