Skip to content

Commit 8bda6ce

Browse files
committed
Add sniff to check if file is executable
1 parent a740354 commit 8bda6ce

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<documentation title="File Permissions">
2+
<standard>
3+
<![CDATA[
4+
Files should not be executable.
5+
]]>
6+
</standard>
7+
</documentation>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Tests that files are not executable.
4+
*
5+
* @author Matthew Peveler <matt.peveler@gmail.com>
6+
* @copyright 2019 Matthew Peveler
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Files;
11+
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use PHP_CodeSniffer\Files\File;
14+
15+
class FileExtensionSniff implements Sniff
16+
{
17+
/**
18+
* Returns an array of tokens this test wants to listen for.
19+
*
20+
* @return array
21+
*/
22+
public function register()
23+
{
24+
return [T_OPEN_TAG];
25+
26+
}//end register()
27+
28+
29+
/**
30+
* Processes this test, when one of its tokens is encountered.
31+
*
32+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
33+
* @param int $stackPtr The position of the current token in the
34+
* stack passed in $tokens.
35+
*
36+
* @return int
37+
*/
38+
public function process(File $phpcsFile, $stackPtr)
39+
{
40+
$perms = fileperms($phpcsFile->getFilename());
41+
42+
if ($perms & 0x0040 || $perms & 0x0008 || $perms & 0x0001) {
43+
$error = "A PHP file must not be executable";
44+
$phpcsFile->addError($error, $stackPtr, 'Executable');
45+
}
46+
47+
// Ignore the rest of the file.
48+
return ($phpcsFile->numTokens + 1);
49+
50+
}//end process()
51+
52+
53+
}//end class

0 commit comments

Comments
 (0)