Skip to content

Commit

Permalink
Fix coding standards
Browse files Browse the repository at this point in the history
This brings things into alignment with MediaWiki coding standards
and also ignores local phpunit.xml files.
  • Loading branch information
samwilson authored and addshore committed May 3, 2017
1 parent fea7165 commit 246f0f5
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 280 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
vendor
composer.lock
test.php
docs/_build
docs/_build
phpunit.xml
42 changes: 23 additions & 19 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@
*/
return [

// A list of directories that should be parsed for class and
// method information. After excluding the directories
// defined in exclude_analysis_directory_list, the remaining
// files will be statically analyzed for errors.
//
// Thus, both first-party and third-party code being used by
// your application should be included in this list.
/**
* A list of directories that should be parsed for class and
* method information. After excluding the directories
* defined in exclude_analysis_directory_list, the remaining
* files will be statically analyzed for errors.
*
* Thus, both first-party and third-party code being used by
* your application should be included in this list.
*/
'directory_list' => [
'src',
'vendor',
],

// A directory list that defines files that will be excluded
// from static analysis, but whose class and method
// information should be included.
//
// Generally, you'll want to include the directories for
// third-party code (such as "vendor/") in this list.
//
// n.b.: If you'd like to parse but not analyze 3rd
// party code, directories containing that code
// should be added to the `directory_list` as
// to `exclude_analysis_directory_list`.
/**
* A directory list that defines files that will be excluded
* from static analysis, but whose class and method
* information should be included.
*
* Generally, you'll want to include the directories for
* third-party code (such as "vendor/") in this list.
*
* n.b.: If you'd like to parse but not analyze 3rd
* party code, directories containing that code
* should be added to the `directory_list` as
* to `exclude_analysis_directory_list`.
*/
"exclude_analysis_directory_list" => [
'vendor/'
],
];
];
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ before_script:

script:
- composer lint
- composer phpcs
- composer phpunit-coverage

after_success:
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require-dev": {
"phpunit/phpunit": "~4.8.0|~5.3.0",
"jakub-onderka/php-parallel-lint": "0.9.2"
"jakub-onderka/php-parallel-lint": "0.9.2",
"mediawiki/mediawiki-codesniffer": "^0.7"
},
"suggest": {
"etsy/phan": "Allows running static analysis on the package (requires PHP 7+)"
Expand All @@ -26,8 +27,10 @@
"phpunit-unit": "phpunit --testsuite unit",
"phpunit-integration": "phpunit --testsuite integration",
"phpunit-coverage": "phpunit --coverage-clover=coverage.clover",
"phpcs": "phpcs -ps",
"test": [
"@lint",
"@phpcs",
"@phpunit"
]
},
Expand Down
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="MediaWiki">
<rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/>
<file>.</file>
<exclude-pattern>vendor/</exclude-pattern>
</ruleset>
7 changes: 4 additions & 3 deletions src/ApiUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class ApiUser {
* @throws \InvalidArgumentException
*/
public function __construct( $username, $password, $domain = null ) {
if( !is_string( $username ) || !is_string( $password ) || !( is_null( $domain ) || is_string( $domain ) ) ) {
$domainIsStringOrNull = ( is_string( $domain ) || is_null( $domain ) );
if ( !is_string( $username ) || !is_string( $password ) || !$domainIsStringOrNull ) {
throw new InvalidArgumentException( 'Username, Password and Domain must all be strings' );
}
if( empty( $username ) || empty( $password ) ) {
if ( empty( $username ) || empty( $password ) ) {
throw new InvalidArgumentException( 'Username and Password are not allowed to be empty' );
}
$this->username = $username;
Expand Down Expand Up @@ -86,4 +87,4 @@ public function equals( $other ) {
&& $this->domain == $other->getDomain();
}

}
}
4 changes: 2 additions & 2 deletions src/FluentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class FluentRequest implements Request {
/**
* @var array
*/
private $params = array();
private $params = [];

/**
* @var array
*/
private $headers = array();
private $headers = [];

/**
* @since 1.0
Expand Down
20 changes: 10 additions & 10 deletions src/Guzzle/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ClientFactory implements LoggerAwareInterface {
* middleware => array of extra middleware to pass to guzzle
* user-agent => string default user agent to use for requests
*/
public function __construct( array $config = array() ) {
public function __construct( array $config = [] ) {
$this->logger = new NullLogger();
$this->config = $config;
}
Expand All @@ -38,7 +38,7 @@ public function __construct( array $config = array() ) {
* @return Client
*/
public function getClient() {
if( $this->client === null ) {
if ( $this->client === null ) {
$this->client = $this->newClient();
}
return $this->client;
Expand All @@ -48,22 +48,22 @@ public function getClient() {
* @return Client
*/
private function newClient() {
$this->config += array(
$this->config += [
'cookies' => true,
'headers' => array(),
'middleware' => array(),
);
'headers' => [],
'middleware' => [],
];

if( !array_key_exists( 'User-Agent', $this->config['headers'] ) ) {
if( array_key_exists( 'user-agent', $this->config ) ) {
if ( !array_key_exists( 'User-Agent', $this->config['headers'] ) ) {
if ( array_key_exists( 'user-agent', $this->config ) ) {
$this->config['headers']['User-Agent'] = $this->config['user-agent'];
} else {
$this->config['headers']['User-Agent'] = 'Addwiki - mediawiki-api-base';
}
}
unset( $this->config['user-agent'] );

if( !array_key_exists( 'handler', $this->config ) ) {
if ( !array_key_exists( 'handler', $this->config ) ) {
$this->config['handler'] = HandlerStack::create( new CurlHandler() );
}

Expand All @@ -72,7 +72,7 @@ private function newClient() {

$this->config['middleware'][] = $middlewareFactory->retry();

foreach( $this->config['middleware'] as $name => $middleware ) {
foreach ( $this->config['middleware'] as $name => $middleware ) {
$this->config['handler']->push( $middleware );
}
unset( $this->config['middleware'] );
Expand Down
22 changes: 11 additions & 11 deletions src/Guzzle/MiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setLogger( LoggerInterface $logger ) {
* @return callable
*/
public function retry( $delay = true ) {
if( $delay ) {
if ( $delay ) {
return Middleware::retry( $this->newRetryDecider(), $this->getRetryDelay() );
} else {
return Middleware::retry( $this->newRetryDecider() );
Expand All @@ -55,16 +55,16 @@ public function retry( $delay = true ) {
private function getRetryDelay() {
return function( $numberOfRetries, Response $response = null ) {
// The $response argument is only passed as of Guzzle 6.2.2.
if( $response !== null ) {
if ( $response !== null ) {
// Retry-After may be a number of seconds or an absolute date (RFC 7231,
// section 7.1.3).
$retryAfter = $response->getHeaderLine( 'Retry-After' );

if( is_numeric( $retryAfter ) ) {
if ( is_numeric( $retryAfter ) ) {
return 1000 * $retryAfter;
}

if( $retryAfter ) {
if ( $retryAfter ) {
$seconds = strtotime( $retryAfter ) - time();
return 1000 * max( 1, $seconds );
}
Expand Down Expand Up @@ -92,19 +92,19 @@ private function newRetryDecider() {
$shouldRetry = false;

// Retry connection exceptions
if( $exception instanceof ConnectException ) {
if ( $exception instanceof ConnectException ) {
$shouldRetry = true;
}

if( $response ) {
if ( $response ) {
$data = json_decode( $response->getBody(), true );

// Retry on server errors
if( $response->getStatusCode() >= 500 ) {
if ( $response->getStatusCode() >= 500 ) {
$shouldRetry = true;
}

foreach( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) {
foreach ( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) {
if (
// Retry if the API explicitly tells us to:
// https://www.mediawiki.org/wiki/Manual:Maxlag_parameter
Expand All @@ -113,12 +113,12 @@ private function newRetryDecider() {
// Retry if we have a response with an API error worth retrying
in_array(
$mediawikiApiErrorHeader,
array(
[
'ratelimited',
'maxlag',
'readonly',
'internal_api_error_DBQueryError',
)
]
)
||
// Or if we have been stopped from saving as an 'anti-abuse measure'
Expand All @@ -135,7 +135,7 @@ private function newRetryDecider() {
}

// Log if we are retrying
if( $shouldRetry ) {
if ( $shouldRetry ) {
$this->logger->warning(
sprintf(
'Retrying %s %s %s/5, %s',
Expand Down
Loading

0 comments on commit 246f0f5

Please sign in to comment.