Skip to content

Commit 5960500

Browse files
committed
Merge branch 'file_executable' of https://github.com/MasterOdin/PHP_CodeSniffer
2 parents adda8bc + bf168df commit 5960500

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
221221
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkStandard.xml" role="php" />
222222
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNewlineStandard.xml" role="php" />
223223
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineStandard.xml" role="php" />
224+
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsStandard.xml" role="php" />
224225
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLStandard.xml" role="php" />
225226
<file baseinstalldir="PHP/CodeSniffer" name="LineEndingsStandard.xml" role="php" />
226227
<file baseinstalldir="PHP/CodeSniffer" name="LineLengthStandard.xml" role="php" />
@@ -323,6 +324,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
323324
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkSniff.php" role="php" />
324325
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNewlineSniff.php" role="php" />
325326
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineSniff.php" role="php" />
327+
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsSniff.php" role="php" />
326328
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLSniff.php" role="php" />
327329
<file baseinstalldir="PHP/CodeSniffer" name="LineEndingsSniff.php" role="php" />
328330
<file baseinstalldir="PHP/CodeSniffer" name="LineLengthSniff.php" role="php" />
@@ -517,6 +519,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
517519
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.6.inc.fixed" role="test" />
518520
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.7.inc" role="test" />
519521
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.php" role="test" />
522+
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.1.inc" role="test" />
523+
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.2.inc" role="test" />
524+
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.php" role="test" />
520525
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.1.inc" role="test" />
521526
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.2.inc" role="test" />
522527
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.3.inc" role="test" />
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 FilePermissionsSniff implements Sniff
16+
{
17+
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return [T_OPEN_TAG];
27+
28+
}//end register()
29+
30+
31+
/**
32+
* Processes this test, when one of its tokens is encountered.
33+
*
34+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35+
* @param int $stackPtr The position of the current token in the
36+
* stack passed in $tokens.
37+
*
38+
* @return int
39+
*/
40+
public function process(File $phpcsFile, $stackPtr)
41+
{
42+
$filename = $phpcsFile->getFilename();
43+
44+
if ($filename !== 'STDIN') {
45+
$perms = fileperms($phpcsFile->getFilename());
46+
47+
if (($perms & 0x0040) !== 0 || ($perms & 0x0008) !== 0 || ($perms & 0x0001) !== 0) {
48+
$error = "A PHP file should not be executable. Found file permission set to %s.";
49+
$data = [substr(sprintf('%o', $perms), -4)];
50+
$phpcsFile->addError($error, 0, 'Executable', $data);
51+
}
52+
}
53+
54+
// Ignore the rest of the file.
55+
return ($phpcsFile->numTokens + 1);
56+
57+
}//end process()
58+
59+
60+
}//end class
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Unit test class for the FilePermissions sniff.
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\Tests\Files;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class FilePermissionsUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @param string $testFile The name of the file being tested.
25+
*
26+
* @return array<int, int>
27+
*/
28+
public function getErrorList($testFile='')
29+
{
30+
switch ($testFile) {
31+
case 'FilePermissionsUnitTest.2.inc':
32+
return [1 => 1];
33+
default:
34+
return [];
35+
}//end switch
36+
37+
}//end getErrorList()
38+
39+
40+
/**
41+
* Returns the lines where warnings should occur.
42+
*
43+
* The key of the array should represent the line number and the value
44+
* should represent the number of warnings that should occur on that line.
45+
*
46+
* @param string $testFile The name of the file being tested.
47+
*
48+
* @return array<int, int>
49+
*/
50+
public function getWarningList($testFile='')
51+
{
52+
return [];
53+
54+
}//end getWarningList()
55+
56+
57+
}//end class

0 commit comments

Comments
 (0)