-
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.
Add sniff to check if file is executable
- Loading branch information
1 parent
a740354
commit 8bda6ce
Showing
2 changed files
with
60 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
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> |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
<?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 FileExtensionSniff 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) | ||
{ | ||
$perms = fileperms($phpcsFile->getFilename()); | ||
|
||
if ($perms & 0x0040 || $perms & 0x0008 || $perms & 0x0001) { | ||
$error = "A PHP file must not be executable"; | ||
$phpcsFile->addError($error, $stackPtr, 'Executable'); | ||
} | ||
|
||
// Ignore the rest of the file. | ||
return ($phpcsFile->numTokens + 1); | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |