diff --git a/.gitattributes b/.gitattributes index 752a0859e..982e87438 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore index 3b596264d..f252a70c5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist new file mode 100644 index 000000000..8381fb896 --- /dev/null +++ b/.phpcs.xml.dist @@ -0,0 +1,270 @@ + + + + Requests rules for PHP_CodeSniffer + + + + + . + + + */library/Requests/IRI\.php$ + */tests/IRI\.php$ + */vendor/* + */tests/coverage/ + + + + + + + + + + + + + + + + + + + + /examples/*\.php$ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /library/*\.php$ + + + + + + + + + + + + + + + + + + + + + + */Transport/cURL\.php$ + */Transport/fsockopen\.php$ + + + + + + + /examples/cookie_jar\.php$ + + + + + /examples/[^/]+\.php$ + + + + + + + /tests/phpunit6-compat\.php$ + + + /tests/phpunit6-compat\.php$ + + + + + /tests/Transport/Base\.php$ + + + + + /tests/Transport/*\.php$ + + + + + /tests/*\.php$ + + + diff --git a/.travis.yml b/.travis.yml index 26d87939d..87ec81c1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -29,6 +46,7 @@ jobs: env: COMPOSER_PHPUNIT=true - php: nightly env: COMPOSER_PHPUNIT=true + allow_failures: - php: nightly env: COMPOSER_PHPUNIT=true @@ -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 diff --git a/composer.json b/composer.json index c2bb58d5e..e4d1a823c 100644 --- a/composer.json +++ b/composer.json @@ -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" + ] } } diff --git a/library/Requests.php b/library/Requests.php index 82ac329e2..997bfa84e 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -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; diff --git a/library/Requests/IDNAEncoder.php b/library/Requests/IDNAEncoder.php index cfa30071e..881142935 100644 --- a/library/Requests/IDNAEncoder.php +++ b/library/Requests/IDNAEncoder.php @@ -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]); diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 92a61abfa..935678391 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -100,9 +100,11 @@ public function __construct() { curl_setopt($this->handle, CURLOPT_ENCODING, ''); } if (defined('CURLOPT_PROTOCOLS')) { + // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_protocolsFound curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); } if (defined('CURLOPT_REDIR_PROTOCOLS')) { + // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_redir_protocolsFound curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); } } @@ -364,6 +366,7 @@ protected function setup_handle($url, $headers, $data, $options) { curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout)); } else { + // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000)); } @@ -371,6 +374,7 @@ protected function setup_handle($url, $headers, $data, $options) { curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout'])); } else { + // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_connecttimeout_msFound curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000)); } curl_setopt($this->handle, CURLOPT_URL, $url); diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 8b59197be..56f94c99f 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -81,6 +81,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $verifyname = true; // SNI, if enabled (OpenSSL >=0.9.8j) + // phpcs:ignore PHPCompatibility.Constants.NewConstants.openssl_tlsext_server_nameFound if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) { $context_options['SNI_enabled'] = true; if (isset($options['verifyname']) && $options['verifyname'] === false) { @@ -117,6 +118,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar } $remote_socket .= ':' . $url_parts['port']; + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE); $options['hooks']->dispatch('fsockopen.remote_socket', array(&$remote_socket)); diff --git a/tests/Cookies.php b/tests/Cookies.php index 146ea4ba2..f0f660104 100644 --- a/tests/Cookies.php +++ b/tests/Cookies.php @@ -379,11 +379,11 @@ public static function parseResultProvider() { // Basic parsing array( 'foo=bar', - array( 'name' => 'foo', 'value' => 'bar' ), + array('name' => 'foo', 'value' => 'bar'), ), array( 'bar', - array( 'name' => '', 'value' => 'bar' ), + array('name' => '', 'value' => 'bar'), ), // Expiration diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIterator.php index 3d94e07f8..5e833ccf2 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIterator.php @@ -9,6 +9,7 @@ public function testDeserializeRequestUtilityFilteredIteratorObjects($value) { if (get_class($value) === 'Requests_Utility_FilteredIterator') { $new_value = unserialize($serialized); if (version_compare(PHP_VERSION, '5.3', '>=')) { + // phpcs:ignore PHPCompatibility.Syntax.NewClassMemberAccess.OnNewFound -- Wrapped in version check. $property = (new ReflectionClass('Requests_Utility_FilteredIterator'))->getProperty('callback'); $property->setAccessible(true); $callback_value = $property->getValue($new_value);