[ticket/12723] Add Sniff ensuring PHP files use the correct file header

PHPBB3-12723
This commit is contained in:
Tristan Darricau 2014-06-20 15:02:08 +02:00
parent 70d4ede9b2
commit 32a2c95f90
20 changed files with 216 additions and 288 deletions

View file

@ -77,14 +77,14 @@
<exec command="phpBB/vendor/bin/phpcs <exec command="phpBB/vendor/bin/phpcs
-s -s
--extensions=php --extensions=php
--standard=build/code_sniffer/ruleset-php-strict.xml --standard=build/code_sniffer/ruleset-phpbb-php-strict.xml
--ignore=${project.basedir}/phpBB/phpbb/db/migration/data/v30x/* --ignore=${project.basedir}/phpBB/phpbb/db/migration/data/v30x/*
phpBB/phpbb" phpBB/phpbb"
dir="." returnProperty="retval-php-strict" passthru="true" /> dir="." returnProperty="retval-php-strict" passthru="true" />
<exec command="phpBB/vendor/bin/phpcs <exec command="phpBB/vendor/bin/phpcs
-s -s
--extensions=php --extensions=php
--standard=build/code_sniffer/ruleset-php-legacy.xml --standard=build/code_sniffer/ruleset-phpbb-php-legacy.xml
--ignore=${project.basedir}/phpBB/cache/* --ignore=${project.basedir}/phpBB/cache/*
--ignore=${project.basedir}/phpBB/develop/* --ignore=${project.basedir}/phpBB/develop/*
--ignore=${project.basedir}/phpBB/includes/diff/*.php --ignore=${project.basedir}/phpBB/includes/diff/*.php

View file

@ -13,7 +13,7 @@
/** /**
* Checks that each source file contains the standard header. * Checks that each source file contains the standard header.
* *
* Based on Coding Guidelines 1.ii File Header. * Based on Coding Guidelines 1.ii File Header.
* *
* @package code_sniffer * @package code_sniffer
@ -47,10 +47,10 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
{ {
if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false)
{ {
return; return;
} }
} }
// Fetch next non whitespace token // Fetch next non whitespace token
$tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens();
$start = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); $start = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
@ -66,65 +66,68 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
$phpcsFile->addError('Missing required file doc comment.', $stackPtr); $phpcsFile->addError('Missing required file doc comment.', $stackPtr);
return; return;
} }
// Find comment end token // Find comment end token
$end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1; $end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1;
// If there is no end, skip processing here // If there is no end, skip processing here
if ($end === false) if ($end === false)
{ {
return; return;
} }
// List of found comment tags // List of found comment tags
$tags = array(); $tags = array();
// check comment lines without the first(/**) an last(*/) line // check comment lines without the first(/**) an last(*/) line
for ($i = $start + 1, $c = ($end - $start); $i <= $c; ++$i) for ($i = $start + 1, $c = $end - 1; $i <= $c; ++$i)
{ {
$line = $tokens[$i]['content']; $line = $tokens[$i]['content'];
// Check that each line starts with a '*' // Check that each line starts with a '*'
if (substr($line, 0, 1) !== '*') if (substr($line, 0, 1) !== '*')
{ {
$message = 'The file doc comment should not be idented.'; $message = 'The file doc comment should not be idented.';
$phpcsFile->addWarning($message, $i); $phpcsFile->addWarning($message, $i);
} }
else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0)
{ {
$tags[$match[1]] = array($match[2], $i); if (!isset($tags[$match[1]]))
{
$tags[$match[1]] = array();
}
$tags[$match[1]][] = array($match[2], $i);
} }
} }
// Check that the first and last line is empty // Check that the first and last line is empty
if (trim($tokens[$start + 1]['content']) !== '*') if (trim($tokens[$start + 1]['content']) !== '*')
{ {
$message = 'The first file comment line should be empty.'; $message = 'The first file comment line should be empty.';
$phpcsFile->addWarning($message, ($start + 1)); $phpcsFile->addWarning($message, ($start + 1));
} }
if (trim($tokens[$end - $start]['content']) !== '*') if (trim($tokens[$end - 1]['content']) !== '*')
{ {
$message = 'The last file comment line should be empty.'; $message = 'The last file comment line should be empty.';
$phpcsFile->addWarning($message, ($end - $start)); $phpcsFile->addWarning($message, $end - 1);
} }
$this->processPackage($phpcsFile, $start, $tags); //$this->processPackage($phpcsFile, $start, $tags);
$this->processVersion($phpcsFile, $start, $tags); //$this->processVersion($phpcsFile, $start, $tags);
$this->processCopyright($phpcsFile, $start, $tags); $this->processCopyright($phpcsFile, $start, $tags);
$this->processLicense($phpcsFile, $start, $tags); $this->processLicense($phpcsFile, $start, $tags);
//print_r($tags);
} }
/** /**
* Checks that the tags array contains a valid package tag * Checks that the tags array contains a valid package tag
* *
* @param PHP_CodeSniffer_File $phpcsFile The context source file instance. * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
* @param integer The stack pointer for the first comment token. * @param integer The stack pointer for the first comment token.
* @param array(string=>array) $tags The found file doc comment tags. * @param array(string=>array) $tags The found file doc comment tags.
* *
* @return null * @return null
*/ */
protected function processPackage(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) protected function processPackage(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
{ {
if (!isset($tags['package'])) if (!isset($tags['package']))
@ -134,80 +137,87 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff
} }
else if (preg_match('/^([\w]+)$/', $tags['package'][0]) === 0) else if (preg_match('/^([\w]+)$/', $tags['package'][0]) === 0)
{ {
$message = 'Invalid content found for @package tag.'; $message = 'Invalid content found for @package tag.';
$phpcsFile->addWarning($message, $tags['package'][1]); $phpcsFile->addWarning($message, $tags['package'][1]);
} }
} }
/** /**
* Checks that the tags array contains a valid version tag * Checks that the tags array contains a valid version tag
* *
* @param PHP_CodeSniffer_File $phpcsFile The context source file instance. * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
* @param integer The stack pointer for the first comment token. * @param integer The stack pointer for the first comment token.
* @param array(string=>array) $tags The found file doc comment tags. * @param array(string=>array) $tags The found file doc comment tags.
* *
* @return null * @return null
*/ */
protected function processVersion(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) protected function processVersion(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
{ {
if (!isset($tags['version'])) if (!isset($tags['version']))
{ {
$message = 'Missing require @version tag in file doc comment.'; $message = 'Missing require @version tag in file doc comment.';
$phpcsFile->addError($message, $ptr); $phpcsFile->addError($message, $ptr);
} }
else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0) else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0)
{ {
$message = 'Invalid content found for @version tag, use "$Id: $".'; $message = 'Invalid content found for @version tag, use "$Id: $".';
$phpcsFile->addError($message, $tags['version'][1]); $phpcsFile->addError($message, $tags['version'][1]);
} }
} }
/** /**
* Checks that the tags array contains a valid copyright tag * Checks that the tags array contains a valid copyright tag
* *
* @param PHP_CodeSniffer_File $phpcsFile The context source file instance. * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
* @param integer The stack pointer for the first comment token. * @param integer The stack pointer for the first comment token.
* @param array(string=>array) $tags The found file doc comment tags. * @param array(string=>array) $tags The found file doc comment tags.
* *
* @return null * @return null
*/ */
protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
{ {
if (!isset($tags['copyright'])) $copyright = '(c) phpBB Limited <https://www.phpbb.com>';
{
$message = 'Missing require @copyright tag in file doc comment.'; if (!isset($tags['copyright']))
$phpcsFile->addError($message, $ptr); {
} $message = 'Missing require @copyright tag in file doc comment.';
else if (preg_match('/^\(c\) 2[0-9]{3} phpBB Group\s*$/', $tags['copyright'][0]) === 0) $phpcsFile->addError($message, $ptr);
{ }
$message = 'Invalid content found for @copyright tag, use "(c) <year> phpBB Group".'; else if ($tags['copyright'][0][0] !== $copyright)
$phpcsFile->addError($message, $tags['copyright'][1]); {
} $message = 'Invalid content found for the first @copyright tag, use "' . $copyright . '".';
} $phpcsFile->addError($message, $tags['copyright'][0][1]);
}
/** }
* Checks that the tags array contains a valid license tag
* /**
* @param PHP_CodeSniffer_File $phpcsFile The context source file instance. * Checks that the tags array contains a valid license tag
* @param integer The stack pointer for the first comment token. *
* @param array(string=>array) $tags The found file doc comment tags. * @param PHP_CodeSniffer_File $phpcsFile The context source file instance.
* * @param integer The stack pointer for the first comment token.
* @return null * @param array(string=>array) $tags The found file doc comment tags.
*/ *
protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) * @return null
{ */
$license = 'http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2'; protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags)
{
if (!isset($tags['license'])) $license = 'GNU General Public License, version 2 (GPL-2.0)';
{
$message = 'Missing require @license tag in file doc comment.'; if (!isset($tags['license']))
$phpcsFile->addError($message, $ptr); {
} $message = 'Missing require @license tag in file doc comment.';
else if (trim($tags['license'][0]) !== $license) $phpcsFile->addError($message, $ptr);
{ }
$message = 'Invalid content found for @license tag, use ' else if (sizeof($tags['license']) !== 1)
. '"' . $license . '".'; {
$phpcsFile->addError($message, $tags['license'][1]); $message = 'It must be only one @license tag in file doc comment.';
} $phpcsFile->addError($message, $ptr);
} }
else if (trim($tags['license'][0][0]) !== $license)
{
$message = 'Invalid content found for @license tag, use '
. '"' . $license . '".';
$phpcsFile->addError($message, $tags['license'][0][1]);
}
}
} }

View file

@ -1,19 +0,0 @@
<?php
/**
*
* @package code_sniffer³
* @version $Id: $
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php BSD License
*
*/
?>
<?php
/**
* Broken but not first file doc comment.
*
* @version @package_version@
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @copyright (c) 2007 phpBB Group
*
*/

View file

@ -1,54 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* Unit test class for the EmptyStatement sniff.
*
* @package code_sniffer
* @author Manuel Pichler <mapi@phpundercontrol.org>
*/
class phpbb_Tests_Commenting_FileCommentUnitTest 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.
*
* @return array(int => int)
*/
public function getErrorList()
{
return array(
7 => 1 // BSD License error :)
);
}//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.
*
* @return array(int => int)
*/
public function getWarningList()
{
return array(
4 => 1,
8 => 1
);
}//end getWarningList()
}

View file

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="code_sniffer" basedir="." default="install">
<property name="working.dir" value="${basedir}" />
<property name="target.dir" value="/usr/share/php/PHP/CodeSniffer/Standards" />
<!--
Install phpbb sniff
-->
<target name="install">
<delete dir="${target.dir}/phpbb" />
<mkdir dir="${target.dir}/phpbb"/>
<copy todir="${target.dir}/phpbb">
<fileset file="${working.dir}/phpbbCodingStandard.php" />
</copy>
<copy todir="${target.dir}/phpbb/Sniffs">
<fileset dir="${working.dir}/Sniffs" />
</copy>
</target>
</project>

View file

@ -1,46 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* @ignore
*/
if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) {
throw new PHP_CodeSniffer_Exception(
'Class PHP_CodeSniffer_Standards_CodingStandard not found'
);
}
/**
* Primary class for the phpbb coding standard.
*
* @package code_sniffer
*/
class PHP_CodeSniffer_Standards_phpbb_phpbbCodingStandard extends PHP_CodeSniffer_Standards_CodingStandard
{
/**
* Return a list of external sniffs to include with this standard.
*
* External locations can be single sniffs, a whole directory of sniffs, or
* an entire coding standard. Locations start with the standard name. For
* example:
* PEAR => include all sniffs in this standard
* PEAR/Sniffs/Files => include all sniffs in this dir
* PEAR/Sniffs/Files/LineLengthSniff => include this single sniff
*
* @return array
*/
public function getIncludedSniffs()
{
return array();
}
}

View file

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<ruleset name="phpBB PHP Legacy Standard">
<description>phpBB legacy coding standard for PHP files</description>
<rule ref="./ruleset-php-legacy.xml" />
<!-- THe header file MUST contains:
- the phpBB copyright: (c) phpBB Limited <https://www.phpbb.com>
- the phpBB License: GNU General Public License, version 2 (GPL-2.0) -->
<rule ref="./phpbb/Sniffs/Commenting/FileCommentSniff.php" />
</ruleset>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset name="phpBB PHP Legacy Standard">
<description>phpBB legacy coding standard for PHP files</description>
<rule ref="./ruleset-phpbb-php-legacy.xml" />
<rule ref="./ruleset-php-strict.xml" />
</ruleset>

View file

@ -1,9 +1,14 @@
<?php <?php
/** /**
* *
* @package acp * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/ */
/** /**

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package acp * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */

View file

@ -1,3 +1,14 @@
<?php <?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
phpinfo(); phpinfo();

View file

@ -3,8 +3,8 @@
* *
* This file is part of the phpBB Forum Software package. * This file is part of the phpBB Forum Software package.
* *
* @copyright (c) 2010-2013 Moxiecode Systems AB
* @copyright (c) phpBB Limited <https://www.phpbb.com> * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @copyright (c) 2010-2013 Moxiecode Systems AB
* @license GNU General Public License, version 2 (GPL-2.0) * @license GNU General Public License, version 2 (GPL-2.0)
* *
* For full copyright and license information, please see * For full copyright and license information, please see

View file

@ -3,7 +3,7 @@
* *
* This file is part of the phpBB Forum Software package. * This file is part of the phpBB Forum Software package.
* *
* @copyright (c) phpBB Limited * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0) * @license GNU General Public License, version 2 (GPL-2.0)
* *
* For full copyright and license information, please see * For full copyright and license information, please see

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package migration * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package migration * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package message * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */
@ -12,8 +16,6 @@ namespace phpbb\message;
/** /**
* Class admin_form * Class admin_form
* Displays a message to the user and allows him to send an email * Displays a message to the user and allows him to send an email
*
* @package phpbb\message
*/ */
class admin_form extends form class admin_form extends form
{ {

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package message * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */
@ -11,8 +15,6 @@ namespace phpbb\message;
/** /**
* Abstract class form * Abstract class form
*
* @package phpbb\message
*/ */
abstract class form abstract class form
{ {

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package message * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */
@ -12,8 +16,6 @@ namespace phpbb\message;
/** /**
* Class message * Class message
* Holds all information for an email and sends it in the end * Holds all information for an email and sends it in the end
*
* @package phpbb\message
*/ */
class message class message
{ {

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package message * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */
@ -12,8 +16,6 @@ namespace phpbb\message;
/** /**
* Class topic_form * Class topic_form
* Form used to send topics as notification emails * Form used to send topics as notification emails
*
* @package phpbb\message
*/ */
class topic_form extends form class topic_form extends form
{ {

View file

@ -1,9 +1,13 @@
<?php <?php
/** /**
* *
* @package message * This file is part of the phpBB Forum Software package.
* @copyright (c) 2014 phpBB Group *
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
* *
*/ */
@ -12,8 +16,6 @@ namespace phpbb\message;
/** /**
* Class user_form * Class user_form
* Allows users to send emails to other users * Allows users to send emails to other users
*
* @package phpbb\message
*/ */
class user_form extends form class user_form extends form
{ {