|
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
|
|
|
$cmd = sprintf('read -r LINE < %s && echo $LINE', $file->getFullPath()); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$process = $this->getProcess($cmd); |
|
45
|
|
|
$process->run(); |
|
46
|
|
|
|
|
47
|
|
|
if (! in_array(trim($process->getOutput()), ['<?php', '#!/usr/bin/env php'])) { |
|
48
|
|
|
$message = 'File must begin with `<?php` or `#!/usr/bin/env php`'; |
|
49
|
|
|
$reporter->error($message, $this, $file); |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: