Skip to content

Commit

Permalink
Merge branch 'file_executable' of https://github.com/MasterOdin/PHP_C…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Nov 12, 2019
2 parents adda8bc + bf168df commit 5960500
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNewlineStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LineEndingsStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LineLengthStandard.xml" role="php" />
Expand Down Expand Up @@ -323,6 +324,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ByteOrderMarkSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNewlineSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LineEndingsSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LineLengthSniff.php" role="php" />
Expand Down Expand Up @@ -517,6 +519,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.6.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.7.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="EndFileNoNewlineUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="FilePermissionsUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="InlineHTMLUnitTest.3.inc" role="test" />
Expand Down
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>
60 changes: 60 additions & 0 deletions src/Standards/Generic/Sniffs/Files/FilePermissionsSniff.php
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
57 changes: 57 additions & 0 deletions src/Standards/Generic/Tests/Files/FilePermissionsUnitTest.php
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

0 comments on commit 5960500

Please sign in to comment.