-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'file_executable' of https://github.com/MasterOdin/PHP_C…
- Loading branch information
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<documentation title="File Permissions"> | ||
<standard> | ||
<![CDATA[ | ||
Files should not be executable. | ||
]]> | ||
</standard> | ||
</documentation> |
60 changes: 60 additions & 0 deletions
60
src/Standards/Generic/Sniffs/Files/FilePermissionsSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Tests that files are not executable. | ||
* | ||
* @author Matthew Peveler <matt.peveler@gmail.com> | ||
* @copyright 2019 Matthew Peveler | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class FilePermissionsSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_OPEN_TAG]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the | ||
* stack passed in $tokens. | ||
* | ||
* @return int | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$filename = $phpcsFile->getFilename(); | ||
|
||
if ($filename !== 'STDIN') { | ||
$perms = fileperms($phpcsFile->getFilename()); | ||
|
||
if (($perms & 0x0040) !== 0 || ($perms & 0x0008) !== 0 || ($perms & 0x0001) !== 0) { | ||
$error = "A PHP file should not be executable. Found file permission set to %s."; | ||
$data = [substr(sprintf('%o', $perms), -4)]; | ||
$phpcsFile->addError($error, 0, 'Executable', $data); | ||
} | ||
} | ||
|
||
// Ignore the rest of the file. | ||
return ($phpcsFile->numTokens + 1); | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
1 change: 1 addition & 0 deletions
1
src/Standards/Generic/Tests/Files/FilePermissionsUnitTest.1.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |
1 change: 1 addition & 0 deletions
1
src/Standards/Generic/Tests/Files/FilePermissionsUnitTest.2.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |
57 changes: 57 additions & 0 deletions
57
src/Standards/Generic/Tests/Files/FilePermissionsUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Unit test class for the FilePermissions sniff. | ||
* | ||
* @author Matthew Peveler <matt.peveler@gmail.com> | ||
* @copyright 2019 Matthew Peveler | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Tests\Files; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class FilePermissionsUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList($testFile='') | ||
{ | ||
switch ($testFile) { | ||
case 'FilePermissionsUnitTest.2.inc': | ||
return [1 => 1]; | ||
default: | ||
return []; | ||
}//end switch | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList($testFile='') | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |