Skip to content
Snippets Groups Projects
Verified Commit e81be2da authored by Marcel Behrmann's avatar Marcel Behrmann
Browse files

Added Moodle Code Sniffer

parent 5cb6b35e
Branches
No related tags found
No related merge requests found
Showing
with 3445 additions and 0 deletions
**/vendor
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="PHP" type="php">
<CLASSES>
<root url="file://$MODULE_DIR$/vendor/composer" />
<root url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
</CLASSES>
<SOURCES>
<root url="file://$MODULE_DIR$/vendor/composer" />
<root url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/code-checker.iml" filepath="$PROJECT_DIR$/.idea/code-checker.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/vendor/composer" />
<path value="$PROJECT_DIR$/vendor/squizlabs/php_codesniffer" />
</include_path>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b525e5fc-f3e4-44bc-a98a-1a30b97d532e" name="Default" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ComposerSettings" synchronizationState="SYNCHRONIZE">
<pharConfigPath>$PROJECT_DIR$/composer.json</pharConfigPath>
</component>
<component name="FileColors">
<fileColor scope="Non-Project Files (Material Default)" color="2E3C43" />
<fileColor scope="Non-Project Files (Material Darker)" color="323232" />
<fileColor scope="Non-Project Files (Material Lighter)" color="eae8e8" />
<fileColor scope="Non-Project Files (Material Palenight)" color="2f2e43" />
<fileColor scope="Non-Project Files (Material Oceanic)" color="2E3C43" />
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="run.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/run.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="262">
<caret line="45" column="68" lean-forward="true" selection-start-line="45" selection-start-column="68" selection-end-line="45" selection-end-column="68" />
<folding />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/run.php" />
</list>
</option>
</component>
<component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsGulpfileManager">
<detection-done>true</detection-done>
<sorting>DEFINITION_ORDER</sorting>
</component>
<component name="PhpWorkspaceProjectConfiguration" backward_compatibility_performed="true">
<include_path>
<path value="$PROJECT_DIR$/vendor/composer" />
<path value="$PROJECT_DIR$/vendor/squizlabs/php_codesniffer" />
</include_path>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="503" />
<option name="y" value="-8" />
<option name="width" value="969" />
<option name="height" value="976" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
<showLibraryContents />
<hideEmptyPackages />
<abbreviatePackageNames />
<autoscrollToSource />
<autoscrollFromSource />
<sortByType />
<manualOrder />
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="code-checker" type="b2602c69:ProjectViewProjectNode" />
<item name="code-checker" type="2a2b976b:PhpTreeStructureProvider$1" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
<pane id="Scratches" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="HbShouldOpenHtmlAsHb" value="" />
<property name="settings.editor.selected.configurable" value="web-ide.project.structure" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="ShelveChangesManager" show_recycled="false">
<option name="remove_strategy" value="false" />
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="b525e5fc-f3e4-44bc-a98a-1a30b97d532e" name="Default" comment="" />
<created>1515657382215</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1515657382215</updated>
<workItem from="1515657383671" duration="3540000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="3540000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.110416666" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Docker" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.32979852" sideWeight="0.5021008" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="File Transfer" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.26723224" sideWeight="0.49789914" order="10" side_tool="false" content_ui="tabs" />
<window_info id="Remote Host" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4049479" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/run.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="262">
<caret line="45" column="68" lean-forward="true" selection-start-line="45" selection-start-column="68" selection-end-line="45" selection-end-column="68" />
<folding />
</state>
</provider>
</entry>
</component>
</project>
\ No newline at end of file
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
service_name: travis-ci
.idea/*
/vendor/
composer.phar
composer.lock
*~
filter:
excluded_paths:
- 'Tests/*'
tools:
php_sim: true
php_pdepend: true
php_analyzer: true
sensiolabs_security_checker: true
checks:
php: true
sudo: false
language: php
matrix:
fast_finish: true
include:
- php: 5.2
env: PHPCS_VERSION="1.5.6"
- php: 5.2
env: PHPCS_VERSION="2.9"
- php: 5.3
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="~1.0"
- php: 5.3
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="~1.0"
- php: 5.4
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="~1.0"
- php: 5.4
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="~1.0"
- php: 5.5
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="dev-master"
- php: 5.5
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="dev-master"
- php: 5.5
env: PHPCS_VERSION="2.9.x-dev" COVERALLS_VERSION="dev-master"
- php: 5.6
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="dev-master"
- php: 5.6
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="dev-master"
- php: 5.6
env: PHPCS_VERSION="2.9.x-dev" COVERALLS_VERSION="dev-master"
- php: 7.0
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="dev-master"
- php: 7.0
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="dev-master"
- php: 7.0
env: PHPCS_VERSION="2.9.x-dev" SNIFF=1 COVERALLS_VERSION="dev-master"
- php: 7.1
env: PHPCS_VERSION=">=1.5.1,<2.0" COVERALLS_VERSION="dev-master"
- php: 7.1
env: PHPCS_VERSION=">=2.0,<3.0" COVERALLS_VERSION="dev-master"
- php: 7.1
env: PHPCS_VERSION="2.9.x-dev" COVERALLS_VERSION="dev-master"
- php: nightly
env: PHPCS_VERSION=">=1.5.1,<2.0"
- php: nightly
env: PHPCS_VERSION=">=2.0,<3.0"
- php: nightly
env: PHPCS_VERSION="2.9.x-dev"
allow_failures:
# Allow failures for unstable builds.
- php: nightly
before_install:
- export XMLLINT_INDENT=" "
# PHP 5.3+: set up test environment using Composer.
- if [[ $TRAVIS_PHP_VERSION > "5.2" ]]; then composer self-update; fi
- if [[ $TRAVIS_PHP_VERSION > "5.2" && $TRAVIS_PHP_VERSION != "nightly" ]]; then composer require --dev satooshi/php-coveralls:${COVERALLS_VERSION}; fi
- if [[ $TRAVIS_PHP_VERSION > "5.2" ]]; then composer require squizlabs/php_codesniffer:${PHPCS_VERSION}; fi
- if [[ $TRAVIS_PHP_VERSION > "5.2" ]]; then composer install; fi
# PHP 5.2: set up test environment using git cloning.
- if [[ $TRAVIS_PHP_VERSION < "5.3" ]]; then export PHPCS_DIR=/tmp/phpcs; fi
- if [[ $TRAVIS_PHP_VERSION < "5.3" ]]; then export PHPCS_BIN=$PHPCS_DIR/scripts/phpcs; fi
- if [[ $TRAVIS_PHP_VERSION < "5.3" ]]; then mkdir -p $PHPCS_DIR && git clone --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git -b $PHPCS_VERSION $PHPCS_DIR; fi
- if [[ $TRAVIS_PHP_VERSION < "5.3" ]]; then $PHPCS_BIN --config-set installed_paths "$(dirname "$(pwd)")"; fi
before_script:
- if [[ $TRAVIS_PHP_VERSION > "5.2" && $TRAVIS_PHP_VERSION != "nightly" ]]; then mkdir -p build/logs; fi
- phpenv rehash
script:
# Lint all PHP files against parse errors.
- if [[ $TRAVIS_PHP_VERSION > "5.2" ]]; then ln -s `pwd` vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PHPCompatibility; fi
- if [[ $TRAVIS_PHP_VERSION > "5.2" ]]; then find -L . -path ./Tests/sniff-examples -prune -o -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l; fi
- if [[ $TRAVIS_PHP_VERSION < "5.3" ]]; then find -L . -path ./Tests/sniff-examples -prune -o -path ./Tests/PHPUnit6Compat.php -prune -o -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l; fi
# Check the code style of the code base.
- if [[ "$SNIFF" == "1" ]]; then vendor/bin/phpcs . --standard=./phpcs.xml; fi
# Run the unit tests.
- if [[ $TRAVIS_PHP_VERSION > "5.2" && $TRAVIS_PHP_VERSION != "nightly" ]]; then phpunit --configuration phpunit-travis.xml --coverage-clover build/logs/clover.xml; fi
- if [[ $TRAVIS_PHP_VERSION == "5.2" || $TRAVIS_PHP_VERSION == "nightly" ]]; then phpunit --configuration phpunit.xml; fi
# Validate the xml file.
# @link http://xmlsoft.org/xmllint.html
- if [[ "$SNIFF" == "1" ]]; then xmllint --noout ./ruleset.xml; fi
# Check the code-style consistency of the xml files.
- if [[ "$SNIFF" == "1" ]]; then diff -B ./ruleset.xml <(xmllint --format "./ruleset.xml"); fi
after_success:
- if [[ $TRAVIS_PHP_VERSION > "5.2" && $TRAVIS_PHP_VERSION != "nightly" ]]; then php vendor/bin/coveralls -v -x build/logs/clover.xml; fi
<?php
/**
* PHPCompatibility_AbstractComplexVersionSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PHPCompatibility_AbstractComplexVersionSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
abstract class PHPCompatibility_AbstractComplexVersionSniff extends PHPCompatibility_Sniff implements PHPCompatibility_ComplexVersionInterface
{
/**
* Handle the retrieval of relevant information and - if necessary - throwing of an
* error/warning for an item.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the relevant token in
* the stack.
* @param array $itemInfo Base information about the item.
*
* @return void
*/
public function handleFeature(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo)
{
$itemArray = $this->getItemArray($itemInfo);
$errorInfo = $this->getErrorInfo($itemArray, $itemInfo);
if ($this->shouldThrowError($errorInfo) === true) {
$this->addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo);
}
}
/**
* Determine whether an error/warning should be thrown for an item based on collected information.
*
* @param array $errorInfo Detail information about an item.
*
* @return bool
*/
abstract protected function shouldThrowError(array $errorInfo);
/**
* Get an array of the non-PHP-version array keys used in a sub-array.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array();
}
/**
* Retrieve a subset of an item array containing only the array keys which
* contain PHP version information.
*
* @param array $itemArray Version and other information about an item.
*
* @return array Array with only the version information.
*/
protected function getVersionArray(array $itemArray)
{
return array_diff_key($itemArray, array_flip($this->getNonVersionArrayKeys()));
}
/**
* Get the item name to be used for the creation of the error code and in the error message.
*
* @param array $itemInfo Base information about the item.
* @param array $errorInfo Detail information about an item.
*
* @return string
*/
protected function getItemName(array $itemInfo, array $errorInfo)
{
return $itemInfo['name'];
}
/**
* Get the error message template for a specific sniff.
*
* @return string
*/
abstract protected function getErrorMsgTemplate();
/**
* Allow for concrete child classes to filter the error message before it's passed to PHPCS.
*
* @param string $error The error message which was created.
* @param array $itemInfo Base information about the item this error message applied to.
* @param array $errorInfo Detail information about an item this error message applied to.
*
* @return string
*/
protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
{
return $error;
}
/**
* Allow for concrete child classes to filter the error data before it's passed to PHPCS.
*
* @param array $data The error data array which was created.
* @param array $itemInfo Base information about the item this error message applied to.
* @param array $errorInfo Detail information about an item this error message applied to.
*
* @return array
*/
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
{
return $data;
}
}//end class
<?php
/**
* PHPCompatibility_AbstractNewFeatureSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PPHPCompatibility_AbstractNewFeatureSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
abstract class PHPCompatibility_AbstractNewFeatureSniff extends PHPCompatibility_AbstractComplexVersionSniff
{
/**
* Determine whether an error/warning should be thrown for an item based on collected information.
*
* @param array $errorInfo Detail information about an item.
*
* @return bool
*/
protected function shouldThrowError(array $errorInfo)
{
return ($errorInfo['not_in_version'] !== '');
}
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @param array $itemArray Version and other information about the item.
* @param array $itemInfo Base information about the item.
*
* @return array
*/
public function getErrorInfo(array $itemArray, array $itemInfo)
{
$errorInfo = array(
'not_in_version' => '',
'error' => true,
);
$versionArray = $this->getVersionArray($itemArray);
if (empty($versionArray) === false) {
foreach ($versionArray as $version => $present) {
if ($errorInfo['not_in_version'] === '' && $present === false
&& $this->supportsBelow($version) === true
) {
$errorInfo['not_in_version'] = $version;
}
}
}
return $errorInfo;
}
/**
* Get the error message template for this sniff.
*
* @return string
*/
protected function getErrorMsgTemplate()
{
return '%s is not present in PHP version %s or earlier';
}
/**
* Generates the error or warning for this item.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the relevant token in
* the stack.
* @param array $itemInfo Base information about the item.
* @param array $errorInfo Array with detail (version) information
* relevant to the item.
*
* @return void
*/
public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
{
$itemName = $this->getItemName($itemInfo, $errorInfo);
$error = $this->getErrorMsgTemplate();
$errorCode = $this->stringToErrorCode($itemName).'Found';
$data = array(
$itemName,
$errorInfo['not_in_version'],
);
$error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
$data = $this->filterErrorData($data, $itemInfo, $errorInfo);
$this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
}
}//end class
<?php
/**
* PHPCompatibility_AbstractRemovedFeatureSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PHPCompatibility_AbstractRemovedFeatureSniff.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
abstract class PHPCompatibility_AbstractRemovedFeatureSniff extends PHPCompatibility_AbstractComplexVersionSniff
{
/**
* Determine whether an error/warning should be thrown for an item based on collected information.
*
* @param array $errorInfo Detail information about an item.
*
* @return bool
*/
protected function shouldThrowError(array $errorInfo)
{
return ($errorInfo['deprecated'] !== '' || $errorInfo['removed'] !== '');
}
/**
* Get an array of the non-PHP-version array keys used in a sub-array.
*
* By default, removed feature version arrays, contain an additional 'alternative' array key.
*
* @return array
*/
protected function getNonVersionArrayKeys()
{
return array('alternative');
}
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @param array $itemArray Version and other information about the item.
* @param array $itemInfo Base information about the item.
*
* @return array
*/
public function getErrorInfo(array $itemArray, array $itemInfo)
{
$errorInfo = array(
'deprecated' => '',
'removed' => '',
'alternative' => '',
'error' => false,
);
$versionArray = $this->getVersionArray($itemArray);
if (empty($versionArray) === false) {
foreach ($versionArray as $version => $removed) {
if ($this->supportsAbove($version) === true) {
if ($removed === true && $errorInfo['removed'] === '') {
$errorInfo['removed'] = $version;
$errorInfo['error'] = true;
} else if ($errorInfo['deprecated'] === '') {
$errorInfo['deprecated'] = $version;
}
}
}
}
if (isset($itemArray['alternative']) === true) {
$errorInfo['alternative'] = $itemArray['alternative'];
}
return $errorInfo;
}
/**
* Get the error message template for suggesting an alternative for a specific sniff.
*
* @return string
*/
protected function getAlternativeOptionTemplate()
{
return '; Use %s instead';
}
/**
* Generates the error or warning for this item.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the relevant token in
* the stack.
* @param array $itemInfo Base information about the item.
* @param array $errorInfo Array with detail (version) information
* relevant to the item.
*
* @return void
*/
public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
{
$itemName = $this->getItemName($itemInfo, $errorInfo);
$error = $this->getErrorMsgTemplate();
$errorCode = $this->stringToErrorCode($itemName);
$data = array($itemName);
if ($errorInfo['deprecated'] !== '') {
$error .= 'deprecated since PHP %s and ';
$errorCode .= 'Deprecated';
$data[] = $errorInfo['deprecated'];
}
if ($errorInfo['removed'] !== '') {
$error .= 'removed since PHP %s and ';
$errorCode .= 'Removed';
$data[] = $errorInfo['removed'];
}
// Remove the last 'and' from the message.
$error = substr($error, 0, (strlen($error) - 5));
if ($errorInfo['alternative'] !== '') {
$error .= $this->getAlternativeOptionTemplate();
$data[] = $errorInfo['alternative'];
}
$error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
$data = $this->filterErrorData($data, $itemInfo, $errorInfo);
$this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
}//end addError()
}//end class
This diff is collapsed.
Reporting bugs
--------------
Before reporting a bug, you should check what sniff an error is coming from.
Run `phpcs` with the `-s` flag will show the names of the sniffs with each error.
Bug reports containing a minimal code sample which can be used to reproduce the issue are highly appreciated as those are most easily actionable.
Requesting features
-------------------
The PHPCompatibility standard only concerns itself with cross-version PHP compatibility of code.
When requesting a new feature, please add a link to a relevant page in the PHP Manual / PHP Changelog / PHP RFC website which illustrates the feature you are requesting.
Pull requests
-------------
Contributions in the form of pull requests are very welcome.
To start contributing, fork the repository, create a new branch in your fork, make your intended changes and pull the branch aginst the `master` branch of this repository.
Please make sure that your pull request contains unit tests covering what's being addressed by it.
All code should be compatible with PHPCS 1.5.6 and PHPCS 2.x.
All code should be compatible with PHP 5.1 to PHP nightly.
Running the Sniff Tests
-----------------------
All the sniffs are fully tested with PHPUnit tests. In order to run the tests
on the sniffs, the following installation steps are required.
1. Install the master branch of `PHP_CodeSniffer`
[https://github.com/squizlabs/PHP_CodeSniffer.git].
This can be done with composer by adding the following into
`~/.composer/composer.json`:
{
"require": {
"phpunit/phpunit": "3.7.*",
"squizlabs/php_codesniffer": ">=2.0"
}
}
2. Run the following command to compose in the versions indicated in the above
global composer.json file:
$ composer.phar global install
3. Update your system `$PATH` to include the globally composed files:
$ export PATH=~/.composer/vendor/bin:$PATH
4. Be sure that the `PHPCompatibility` directory is symlinked into
`PHP_Codesniffer`'s standards directory:
$ ln -s /path/to/PHPCompatibility ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PHPCompatibility
5. Verify standard is available with `phpcs -i`. The output should include
`PHPCompatibility`
6. Run the tests by running `phpunit` in the root directory of
PHPCompatibility. It will read the `phpunit.xml` file and execute the tests
#### Issues when running the PHPCS Unit tests for another standard
This sniff library uses its own PHPUnit setup rather than the PHPCS native unit testing framework to allow for testing the sniffs with various config settings for the `testVersion` variable.
If you are running the PHPCS native unit tests or the unit tests for another sniff library which uses the PHPCS native unit testing framework, PHPUnit might throw errors related to this sniff library depending on your setup.
This will generally only happen if you have both PHPCompatibility as well as another custom sniff library in your PHPCS `installed_paths` setting.
To fix these errors, make sure you are running PHPCS 2.7.1 or higher and add the following to the `phpunit.xml` file for the sniff library you are testing:
```xml
<php>
<env name="PHPCS_IGNORE_TESTS" value="PHPCompatibility"/>
</php>
```
This will prevent PHPCS trying to include the PHPCompatibility unit tests when creating the test suite.
<?php
/**
* PHPCompatibility_ComplexVersionInterface.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PHPCompatibility_ComplexVersionInterface.
*
* Interface to be implemented by sniffs using a multi-dimensional array of
* PHP features (functions, classes etc) being sniffed for with version
* information in sub-arrays.
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
interface PHPCompatibility_ComplexVersionInterface
{
/**
* Handle the retrieval of relevant information and - if necessary - throwing of an
* error/warning for an item.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the relevant token in
* the stack.
* @param array $itemInfo Base information about the item.
*
* @return void
*/
public function handleFeature(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo);
/**
* Get the relevant sub-array for a specific item from a multi-dimensional array.
*
* @param array $itemInfo Base information about the item.
*
* @return array Version and other information about the item.
*/
public function getItemArray(array $itemInfo);
/**
* Retrieve the relevant detail (version) information for use in an error message.
*
* @param array $itemArray Version and other information about the item.
* @param array $itemInfo Base information about the item.
*
* @return array
*/
public function getErrorInfo(array $itemArray, array $itemInfo);
/**
* Generates the error or warning for this item.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the relevant token in
* the stack.
* @param array $itemInfo Base information about the item.
* @param array $errorInfo Array with detail (version) information
* relevant to the item.
*
* @return void
*/
public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo);
}//end interface
PHP Compatibility Coding Standard for PHP_CodeSniffer
=====================================================
[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=wimg&url=https://github.com/wimg/PHPCompatibility&title=PHPCompatibility&language=&tags=github&category=software)
[![Build Status](https://travis-ci.org/wimg/PHPCompatibility.png?branch=master)](https://travis-ci.org/wimg/PHPCompatibility)
[![Coverage Status](https://coveralls.io/repos/github/wimg/PHPCompatibility/badge.svg?branch=master)](https://coveralls.io/github/wimg/PHPCompatibility?branch=master)
[![Latest Stable Version](https://poser.pugx.org/wimg/php-compatibility/v/stable.png)](https://packagist.org/packages/wimg/php-compatibility)
[![Latest Unstable Version](https://poser.pugx.org/wimg/php-compatibility/v/unstable.png)](https://packagist.org/packages/wimg/php-compatibility)
[![License](https://poser.pugx.org/wimg/php-compatibility/license.png)](https://packagist.org/packages/wimg/php-compatibility)
This is a set of sniffs for [PHP_CodeSniffer](http://pear.php.net/PHP_CodeSniffer) that checks for PHP version compatibility.
It will allow you to analyse your code for compatibility with higher and lower versions of PHP.
PHP Version Support
-------
The project aims to cover all PHP compatibility changes introduced since PHP 5.0 up to the latest PHP release. This is an ongoing process and coverage is not yet 100% (if, indeed, it ever could be). Progress is tracked on [our Github issue tracker](https://github.com/wimg/PHPCompatibility/issues).
Pull requests that check for compatibility issues in PHP4 code - in particular between PHP 4 and PHP 5.0 - are very welcome as there are still situations where people need help upgrading legacy systems. However, coverage for changes introduced before PHP 5.1 will remain patchy as sniffs for this are not actively being developed at this time.
Requirements
-------
The sniffs are designed to give the same results regardless of which PHP version you are using to run CodeSniffer. You should get reasonably consistent results independently of the PHP version used in your test environment, though for the best results it is recommended to run the sniffs on PHP 5.3 or higher.
PHP CodeSniffer 1.5.1 is required for 90% of the sniffs, PHPCS 2.6 or later is required for full support, notices may be thrown on older versions.
The PHPCompatibility standard is currently not compatible with PHPCS 3.0, though the [intention is to fix this](https://github.com/wimg/PHPCompatibility/issues/367) in the near future.
Thank you
---------
Thanks to all contributors for their valuable contributions.
[![WPEngine](https://cu.be/img/wpengine.png)](https://wpengine.com)
Thanks to [WP Engine](https://wpengine.com) for their support on the PHP 7.0 sniffs.
Installation using PEAR (method 1)
-----------------------
* Install [PHP_CodeSniffer](http://pear.php.net/PHP_CodeSniffer) with `pear install PHP_CodeSniffer`.
* Checkout the latest release from https://github.com/wimg/PHPCompatibility/releases into the `PHP/CodeSniffer/Standards/PHPCompatibility` directory.
Installation in Composer project (method 2)
-------------------------------------------
* Add the following lines to the `require-dev` section of your composer.json file.
```json
"require-dev": {
"squizlabs/php_codesniffer": "*",
"wimg/php-compatibility": "*",
"simplyadmire/composer-plugins" : "@dev"
},
"prefer-stable" : true
```
* Run `composer update --lock` to install both phpcs and PHPCompatibility coding standard.
* Use the coding standard with `./vendor/bin/phpcs --standard=PHPCompatibility`
Installation via a git check-out to an arbitrary directory (method 3)
-----------------------
* Install [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) via [your preferred method](https://github.com/squizlabs/PHP_CodeSniffer#installation) (Composer, PEAR, Phar file, Git checkout).
* Checkout the latest release from https://github.com/wimg/PHPCompatibility/releases into an arbitrary directory.
* Add the path to the directory **_above_** the directory in which you cloned the PHPCompability repo to the PHPCS configuration using the below command.
```bash
phpcs --config-set installed_paths /path/to/dir/above
```
I.e. if you cloned the `PHPCompatibility` repository to the `/my/custom/standards/PHPCompatibility` directory, you will need to add the `/my/custom/standards` directory to the PHPCS [`installed_paths` configuration variable](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-installed-standard-paths).
**Pro-tip:** Alternatively, _and only if you use PHPCS version 2.6.0 or higher_, you can tell PHP_CodeSniffer the path to the PHPCompatibility standard by adding the following snippet to your custom ruleset:
```xml
<config name="installed_paths" value="/path/to/dir/above" />
```
Using the compatibility sniffs
------------------------------
* Use the coding standard with `phpcs --standard=PHPCompatibility`
* You can specify which PHP version you want to test against by specifying `--runtime-set testVersion 5.5`.
* You can also specify a range of PHP versions that your code needs to support. In this situation, compatibility issues that affect any of the PHP versions in that range will be reported:
`--runtime-set testVersion 5.3-5.5`. You can omit one or other part of the range if you want to support everything above/below a particular version (e.g. `--runtime-set testVersion 7.0-` to support PHP 7 and above).
More information can be found on Wim Godden's [blog](http://techblog.wimgodden.be/tag/codesniffer).
Using a custom ruleset
------------------------------
Alternatively, you can add PHPCompatibility to a custom PHPCS ruleset.
```xml
<?xml version="1.0"?>
<ruleset name="Custom ruleset">
<description>My rules for PHP_CodeSniffer</description>
<!-- Run against the PHPCompatibility ruleset -->
<rule ref="PHPCompatibility" />
<!-- Run against a second ruleset -->
<rule ref="PSR2" />
</ruleset>
```
You can also set the `testVersion` from within the ruleset:
```xml
<config name="testVersion" value="5.3-5.5"/>
```
Other advanced options, such as changing the message type or severity of select sniffs, as described in the [PHPCS Annotated ruleset](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml) wiki page are, of course, also supported.
#### PHPCompatibility specific options
At this moment, there is one sniff which has a property which can be set via the ruleset. More custom properties may become available in the future.
The `PHPCompatibility.PHP.RemovedExtensions` sniff checks for removed extensions based on the function prefix used for these extensions.
This might clash with userland functions using the same function prefix.
To whitelist userland functions, you can pass a comma-delimited list of function names to the sniff.
```xml
<!-- Whitelist the mysql_to_rfc3339() and mysql_another_function() functions. -->
<rule ref="PHPCompatibility.PHP.RemovedExtensions">
<properties>
<property name="functionWhitelist" type="array" value="mysql_to_rfc3339,mysql_another_function" />
</properties>
</rule>
```
License
-------
This code is released under the GNU Lesser General Public License (LGPL). For more information, visit http://www.gnu.org/copyleft/lesser.html
This diff is collapsed.
<?php
/**
* PHPCompatibility_Sniffs_PHP_CaseSensitiveKeywordsSniff.
*
* PHP version 5.5
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PHPCompatibility_Sniffs_PHP_CaseSensitiveKeywordsSniff.
*
* Prior to PHP 5.5, cases existed where the self, parent, and static keywords
* were treated in a case sensitive fashion.
*
* PHP version 5.5
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class PHPCompatibility_Sniffs_PHP_CaseSensitiveKeywordsSniff extends PHPCompatibility_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_SELF,
T_STATIC,
T_PARENT,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsBelow('5.4') === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$tokenContentLC = strtolower($tokens[$stackPtr]['content']);
if ($tokenContentLC !== $tokens[$stackPtr]['content']) {
$phpcsFile->addError(
'The keyword \'%s\' was treated in a case-sensitive fashion in certain cases in PHP 5.4 or earlier. Use the lowercase version for consistent support.',
$stackPtr,
'NonLowercaseFound',
array($tokenContentLC)
);
}
}
}
<?php
/**
* PHPCompatibility_Sniffs_PHP_ConstantArraysUsingConstSniff.
*
* PHP version 5.6
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
/**
* PHPCompatibility_Sniffs_PHP_ConstantArraysUsingConstSniff.
*
* Constant arrays using the constant keyword in PHP 5.6
*
* PHP version 5.6
*
* @category PHP
* @package PHPCompatibility
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl>
*/
class PHPCompatibility_Sniffs_PHP_ConstantArraysUsingConstSniff extends PHPCompatibility_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_CONST);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsBelow('5.5') !== true) {
return;
}
$find = array(
T_ARRAY => T_ARRAY,
T_OPEN_SHORT_ARRAY => T_OPEN_SHORT_ARRAY,
T_CLOSE_SHORT_ARRAY => T_CLOSE_SHORT_ARRAY,
);
$hasArray = $phpcsFile->findNext($find, ($stackPtr + 1), null, false, null, true);
if ($hasArray !== false) {
$phpcsFile->addError(
'Constant arrays using the "const" keyword are not allowed in PHP 5.5 or earlier',
$hasArray,
'Found'
);
}
}
}
<?php
/**
* PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim@cu.be>
*/
/**
* PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff.
*
* Constant arrays using define in PHP 7.0
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim@cu.be>
*/
class PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff extends PHPCompatibility_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_STRING);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsBelow('5.6') !== true) {
return;
}
$tokens = $phpcsFile->getTokens();
$ignore = array(
T_DOUBLE_COLON,
T_OBJECT_OPERATOR,
T_FUNCTION,
T_CONST,
);
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
// Not a call to a PHP function.
return;
}
$functionLc = strtolower($tokens[$stackPtr]['content']);
if ($functionLc !== 'define') {
return;
}
$secondParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 2);
if (isset($secondParam['start'], $secondParam['end']) === false) {
return;
}
$targetNestingLevel = 0;
if (isset($tokens[$secondParam['start']]['nested_parenthesis'])) {
$targetNestingLevel = count($tokens[$secondParam['start']]['nested_parenthesis']);
}
$array = $phpcsFile->findNext(array(T_ARRAY, T_OPEN_SHORT_ARRAY), $secondParam['start'], ($secondParam['end'] + 1));
if ($array !== false) {
if ((isset($tokens[$array]['nested_parenthesis']) === false && $targetNestingLevel === 0) || count($tokens[$array]['nested_parenthesis']) === $targetNestingLevel) {
$phpcsFile->addError(
'Constant arrays using define are not allowed in PHP 5.6 or earlier',
$array,
'Found'
);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment