Skip to content

Commit

Permalink
Add sniff to check if file is executable
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Aug 11, 2019
1 parent a740354 commit 8bda6ce
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Standards/Generic/Docs/Files/FilePermissionsStandard.xml
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 src/Standards/Generic/Sniffs/Files/FilePermissionsSniff.php
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

0 comments on commit 8bda6ce

Please sign in to comment.