Skip to content

Commit

Permalink
Merge pull request #434 from rmccue/feature/add-cs-check
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Nov 17, 2020
2 parents 77fb723 + 39316fe commit b304eef
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ tests/ export-ignore
.coveralls.yml export-ignore
.gitignore export-ignore
.gitattributes export-ignore
.phpcs.xml.dist export-ignore
.travis.yml export-ignore
package.xml.tpl export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ tests/coverage/*
# Ignore composer related files
/composer.lock
/vendor

# Ignore local overrides of the PHPCS config file.
.phpcs.xml
phpcs.xml
270 changes: 270 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="Requests"
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">

<description>Requests rules for PHP_CodeSniffer</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
#############################################################################
-->

<!-- Scan all files. -->
<file>.</file>

<!-- Third party files and build files don't need to comply with these coding standards. -->
<exclude-pattern>*/library/Requests/IRI\.php$</exclude-pattern>
<exclude-pattern>*/tests/IRI\.php$</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/coverage/</exclude-pattern>

<!-- Only check PHP files. -->
<arg name="extensions" value="php"/>

<!-- Show progress, show the error codes for each message (source). -->
<arg value="ps"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>


<!--
#############################################################################
CHECK FOR PHP CROSS-VERSION COMPATIBILITY
#############################################################################
-->

<config name="testVersion" value="5.2-"/>
<rule ref="PHPCompatibility">
<!-- The example code may contain code samples targetted at PHP > 5.2. -->
<exclude-pattern>/examples/*\.php$</exclude-pattern>
</rule>


<!--
#############################################################################
CODING STYLE RULES
#############################################################################
-->

<!-- Use the WordPress Coding Standards as a basis, but with tweaks. -->
<rule ref="WordPress-Extra">
<!-- No need for this sniff as PHP linting is included in the CI builds against
multiple PHP versions (which is the better solution). -->
<exclude name="Generic.PHP.Syntax"/>

<!-- ==========================================================================
No "nacin-spacing". I.e. don't enforce whitespace on the inside of braces and such.
========================================================================== -->

<!-- Replace by the Squiz version of the sniff which is space-poor in contrast
to the space-rich WPCS sniff. -->
<exclude name="WordPress.WhiteSpace.ControlStructureSpacing"/>

<!-- Replaced by the Squiz version of the sniff which doesn't enforce whitespace
around the boolean not `!` operator and enforcement via the Generic sniff. -->
<exclude name="WordPress.WhiteSpace.OperatorSpacing"/>

<!-- Replaced by the Squiz version of the sniff which doesn't enforce whitespace around array index keys. -->
<exclude name="WordPress.Arrays.ArrayKeySpacingRestrictions"/>

<!-- Disabled as the preference for this repo is no spaces on the inside of parentheses.
Once WPCS 3.0.0 comes out, the opposite can be enforced via the
NormalizedArrays.Arrays.ArrayBraceSpacing sniff from PHPCSExtra. -->
<exclude name="WordPress.Arrays.ArrayDeclarationSpacing.NoSpaceAfterArrayOpener"/>
<exclude name="WordPress.Arrays.ArrayDeclarationSpacing.NoSpaceBeforeArrayCloser"/>

<!-- Let spacing before a cast be determined by the operator/parentheses whitespace rule
of the preceding character. -->
<exclude name="WordPress.WhiteSpace.CastStructureSpacing"/>

<!-- ==========================================================================
Forbid "assignments in conditions" instead of enforcing Yoda conditions.
========================================================================== -->
<exclude name="WordPress.PHP.YodaConditions"/>
<!-- A while loop is the only valid control structure where an assignment can be justified. -->
<exclude name="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition"/>

<!-- ==========================================================================
Miscellaneous other exclusions.
========================================================================== -->

<!-- This repo complies with PSR 0 for filename conventions. -->
<exclude name="WordPress.Files.FileName"/>

<!-- This code base consistently has the second keyword of multi-part control structures
on a new line.
Once WPCS 3.0.0 comes out, the style used in this repo can be enforced via the
Universal.ControlStructures.IfElseDeclaration sniff from PHPCSExtra. -->
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>

<!-- Ignore WP specific sniffs as Requests is also used outside of a WP context. -->
<exclude name="WordPress.WP"/>
<exclude name="WordPress.DateTime"/>
<exclude name="WordPress.Security"/>
<exclude name="WordPress.PHP.DiscouragedPHPFunctions"/>
</rule>


<!-- ==========================================================================
Enforcement of space-poor code style.
========================================================================== -->

<!-- Overrule the properties set in the WP Core ruleset for several sniffs. -->
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
<property name="requiredSpacesAfterOpen" value="0"/>
<property name="requiredSpacesBeforeClose" value="0"/>
</properties>
<!-- Undo the WPCS severity change (exclusion) for spacing before close -->
<severity>5</severity>
</rule>

<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="0"/>
<property name="requiredSpacesBeforeClose" value="0"/>
</properties>
</rule>

<rule ref="Generic.WhiteSpace.ArbitraryParenthesesSpacing">
<properties>
<property name="spacing" value="0"/>
</properties>
</rule>

<!-- Include replacement sniffs which enforce the opposite of WPCS for several excluded sniffs. -->
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing">
<!-- Note: These two violations should potentially be addressed at a later point in time. -->
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.NoLineAfterClose"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.LineAfterClose"/>
</rule>

<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>

<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>

<!-- Enforce no space after the boolean not `!` operator. -->
<rule ref="Generic.Formatting.SpaceAfterNot">
<properties>
<property name="spacing" value="0"/>
</properties>
</rule>

<!-- ==========================================================================
Disallow Yoda conditions.
========================================================================== -->
<rule ref="Generic.ControlStructures.DisallowYodaConditions"/>


<!--
#############################################################################
SNIFF SPECIFIC CONFIGURATION
#############################################################################
-->

<!-- Allow error silencing only for a select group of functions. -->
<rule ref="WordPress.PHP.NoSilencedErrors">
<properties>
<property name="custom_whitelist" type="array">
<element value="gzdecode"/>
<element value="gzinflate"/>
<element value="gzuncompress"/>
</property>
</properties>
</rule>

<!-- Make sure everything in the global namespace is prefixed. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="Requests"/>
</property>
</properties>

<!-- Only code within the actual distributed package needs to be prefixed. -->
<include-pattern>/library/*\.php$</include-pattern>
</rule>

<!-- Simplify alignment rules for multiline arrays. -->
<rule ref="WordPress.Arrays.MultipleStatementAlignment">
<properties>
<!-- No need to adjust alignment of large arrays when the item with the largest key is removed. -->
<property name="exact" value="false"/>
<!-- Don't align multi-line items if ALL items in the array are multi-line. -->
<property name="alignMultilineItems" value="!=100"/>
<!-- The array assignment operator should always be on the same line as the array key. -->
<property name="ignoreNewlines" value="false"/>
</properties>
</rule>


<!--
#############################################################################
SELECTIVE EXCLUSIONS
Exclude specific files for specific sniffs.
#############################################################################
-->

<!-- ==========================================================================
Package code
========================================================================== -->

<!-- Don't rename existing classes to prevent a BC break. -->
<rule ref="PEAR.NamingConventions.ValidClassName.Invalid">
<exclude-pattern>*/Transport/cURL\.php$</exclude-pattern>
<exclude-pattern>*/Transport/fsockopen\.php$</exclude-pattern>
</rule>

<!-- ==========================================================================
Example code
========================================================================== -->

<!-- Using PHP 5.4+ syntax in example code is fine. -->
<rule ref="Generic.Arrays.DisallowShortArraySyntax.Found">
<exclude-pattern>/examples/cookie_jar\.php$</exclude-pattern>
</rule>

<!-- Using var_dump() in example code is fine. -->
<rule ref="WordPress.PHP.DevelopmentFunctions.error_log_var_dump">
<exclude-pattern>/examples/[^/]+\.php$</exclude-pattern>
</rule>

<!-- ==========================================================================
Test code
========================================================================== -->

<!-- The PHPUnit6-compat file is only loaded on PHP 7.0 or higher. -->
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.class_aliasFound">
<exclude-pattern>/tests/phpunit6-compat\.php$</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound">
<exclude-pattern>/tests/phpunit6-compat\.php$</exclude-pattern>
</rule>

<!-- Tests aren't run on PHP 5.2.0 anymore, so just ignore this. -->
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.sys_get_temp_dirFound">
<exclude-pattern>/tests/Transport/Base\.php$</exclude-pattern>
</rule>

<!-- Overloaded methods to make the expected exceptions more specific. -->
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod">
<exclude-pattern>/tests/Transport/*\.php$</exclude-pattern>
</rule>

<!-- Allow single-line multi-item associative arrays in the unit tests. -->
<rule ref="WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound">
<exclude-pattern>/tests/*\.php$</exclude-pattern>
</rule>

</ruleset>
26 changes: 25 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
dist: xenial
language: php

stages:
- sniff
- test

jobs:
fast_finish: true
include:
- php: 5.2
- stage: sniff
php: 7.4
install:
- phpenv config-rm xdebug.ini || echo 'No xdebug config.'
- travis_retry composer install --no-interaction
before_script: skip
script:
# Check the code against the coding standards.
- composer checkcs
after_script: skip

- stage: test
php: 5.2
dist: precise
env: COMPOSER_PHPUNIT=false
- php: 5.3
Expand All @@ -29,6 +46,7 @@ jobs:
env: COMPOSER_PHPUNIT=true
- php: nightly
env: COMPOSER_PHPUNIT=true

allow_failures:
- php: nightly
env: COMPOSER_PHPUNIT=true
Expand All @@ -53,6 +71,12 @@ install:
- if [ "$COMPOSER_PHPUNIT" == 'false' ]; then export PHPUNIT_BIN="phpunit";
else export PHPUNIT_BIN="$(pwd)/vendor/bin/phpunit";
fi
- |
if [[ "${TRAVIS_BUILD_STAGE_NAME^}" != "Sniff" ]]; then
# The PHPCS dependencies require PHP 5.4, so would block istall on PHP < 5.4.
# As they are only needed in the sniff stage, remove them.
travis_retry composer remove --dev --no-update squizlabs/php_codesniffer phpcompatibility/php-compatibility wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer
fi
- travis_retry composer install --dev --no-interaction
- TESTPHPBIN=$(phpenv which php)
- phpenv local --unset
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
},
"require-dev": {
"requests/test-server": "dev-master",
"phpunit/phpunit": "<6.0"
"phpunit/phpunit": "<6.0",
"squizlabs/php_codesniffer": "^3.5",
"phpcompatibility/php-compatibility": "^9.0",
"wp-coding-standards/wpcs": "^2.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7"
},
"type": "library",
"autoload": {
"psr-0": {"Requests": "library/"}
},
"scripts" : {
"checkcs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs"
],
"fixcs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
]
}
}
1 change: 1 addition & 0 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ public static function decompress($data) {
}

if (function_exists('gzdecode')) {
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.gzdecodeFound -- Wrapped in function_exists() for PHP 5.2.
$decoded = @gzdecode($data);
if ($decoded !== false) {
return $decoded;
Expand Down
1 change: 1 addition & 0 deletions library/Requests/IDNAEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ protected static function utf8_to_codepoints($input) {
// Get number of bytes
$strlen = strlen($input);

// phpcs:ignore Generic.CodeAnalysis.JumbledIncrementer -- This is a deliberate choice.
for ($position = 0; $position < $strlen; $position++) {
$value = ord($input[$position]);

Expand Down

0 comments on commit b304eef

Please sign in to comment.