Skip to content

Commit

Permalink
Use rector (#43)
Browse files Browse the repository at this point in the history
* Start trying rector

* Another fix

* Actually add rector.php...

* rector: phpunit & php7 sets

* rector: Option::AUTO_IMPORT_NAMES, true

* rector SetList::CODE_QUALITY_STRICT

* rector, include more files

* rector code style

* rector: SetList::DEAD_CODE

* Fix removal of addKeyPrefixIfNeeded in FluentGenerator

* wikibase-api phpcs skip MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation

* Revert "rector code style"

This reverts commit 5b511c5b5d17aa38c2376d77cf9770db648685eb.

This caused issues in test somewhere...
We will need to progress slower to figure out what caused it?

* Re add most of rector code style

* more rector code style 1

* MakeInheritedMethodVisibilitySameAsParentRector

* style fix

* AddArrayDefaultToArrayPropertyRector with fix

* AddFalseDefaultToBoolPropertyRector

* Github Action for rector
  • Loading branch information
addwiki-ci committed Feb 16, 2021
1 parent 23c2dd0 commit 86f05f0
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/ApiUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ApiUser {
* @param string $password The user's password.
* @param string|null $domain The domain (for authentication systems that support domains).
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct( $username, $password, $domain = null ) {
$domainIsStringOrNull = ( is_string( $domain ) || $domain === null );
Expand Down Expand Up @@ -82,9 +82,9 @@ public function getDomain() {
*/
public function equals( $other ) {
return $other instanceof self
&& $this->username == $other->getUsername()
&& $this->password == $other->getPassword()
&& $this->domain == $other->getDomain();
&& $this->username === $other->getUsername()
&& $this->password === $other->getPassword()
&& $this->domain === $other->getDomain();
}

}
2 changes: 1 addition & 1 deletion src/Guzzle/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function newClient() {

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

foreach ( $this->config['middleware'] as $name => $middleware ) {
foreach ( $this->config['middleware'] as $middleware ) {
$this->config['handler']->push( $middleware );
}
unset( $this->config['middleware'] );
Expand Down
9 changes: 5 additions & 4 deletions src/Guzzle/MiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function getRetryDelay() {
return 1000 * $retryAfter;
}

if ( $retryAfter ) {
if ( $retryAfter !== '' ) {
$seconds = strtotime( $retryAfter ) - time();
return 1000 * max( 1, $seconds );
}
Expand Down Expand Up @@ -99,7 +99,7 @@ private function newRetryDecider() {
$shouldRetry = true;
}

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

// Retry on server errors
Expand All @@ -108,10 +108,11 @@ private function newRetryDecider() {
}

foreach ( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) {
$RetryAfterResponseHeaderLine = $response->getHeaderLine( 'Retry-After' );
if (
// Retry if the API explicitly tells us to:
// https://www.mediawiki.org/wiki/Manual:Maxlag_parameter
$response->getHeaderLine( 'Retry-After' )
$RetryAfterResponseHeaderLine
||
// Retry if we have a response with an API error worth retrying
in_array(
Expand Down Expand Up @@ -145,7 +146,7 @@ private function newRetryDecider() {
$request->getMethod(),
$request->getUri(),
$retries + 1,
$response ? 'status code: ' . $response->getStatusCode() :
$response !== null ? 'status code: ' . $response->getStatusCode() :
$exception->getMessage()
)
);
Expand Down
18 changes: 11 additions & 7 deletions src/MediawikiApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface {
/**
* @var ClientInterface|null Should be accessed through getClient
*/
private $client = null;
private $client;

/**
* @var bool|string
Expand Down Expand Up @@ -100,9 +100,9 @@ public static function newFromPage( $url ) {
return $prevErr . ', ' . $err->message . ' (line ' . $err->line . ')';
} );
if ( $libXmlErrorStr ) {
$libXmlErrorStr = "In addition, libxml had the following errors: $libXmlErrorStr";
$libXmlErrorStr = sprintf( 'In addition, libxml had the following errors: %s', $libXmlErrorStr );
}
throw new RsdException( "Unable to find RSD URL in page: $url $libXmlErrorStr" );
throw new RsdException( sprintf( 'Unable to find RSD URL in page: %s %s', $url, $libXmlErrorStr ) );
}
$rsdUrl = $link->item( 0 )->attributes->getnamedItem( 'href' )->nodeValue;

Expand Down Expand Up @@ -187,7 +187,9 @@ public function getRequestAsync( Request $request ) {
);

return $promise->then( function ( ResponseInterface $response ) {
return call_user_func( [ $this, 'decodeResponse' ], $response );
return call_user_func( function ( ResponseInterface $response ) {
return $this->decodeResponse( $response );
}, $response );
} );
}

Expand All @@ -208,7 +210,9 @@ public function postRequestAsync( Request $request ) {
);

return $promise->then( function ( ResponseInterface $response ) {
return call_user_func( [ $this, 'decodeResponse' ], $response );
return call_user_func( function ( ResponseInterface $response ) {
return $this->decodeResponse( $response );
}, $response );
} );
}

Expand Down Expand Up @@ -524,13 +528,13 @@ public function clearTokens() {
* @return string
*/
public function getVersion() {
if ( !isset( $this->version ) ) {
if ( $this->version === null ) {
$result = $this->getRequest( new SimpleRequest( 'query', [
'meta' => 'siteinfo',
'continue' => '',
] ) );
preg_match(
'/\d+(?:\.\d+)+/',
'#\d+(?:\.\d+)+#',
$result['query']['general']['generator'],
$versionParts
);
Expand Down
7 changes: 2 additions & 5 deletions src/MediawikiSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ private function getNewTokenType( $type ) {
* @return string
*/
private function getOldTokenType( $type ) {
switch ( $type ) {
// Guess that we want an edit token, this may not always work as we might be trying to
// use it for something else...
case 'csrf':
return 'edit';
if ( $type === 'csrf' ) {
return 'edit';
}
return $type;
}
Expand Down
6 changes: 3 additions & 3 deletions src/MultipartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class MultipartRequest extends FluentRequest {
protected function checkMultipartParams( $params ) {
foreach ( $params as $key => $val ) {
if ( !is_array( $val ) ) {
throw new Exception( "Parameter '$key' must be an array." );
throw new Exception( sprintf( "Parameter '%s' must be an array.", $key ) );
}
if ( !in_array( $key, array_keys( $this->getParams() ) ) ) {
throw new Exception( "Parameter '$key' is not already set on this request." );
if ( !array_key_exists( $key, $this->getParams() ) ) {
throw new Exception( sprintf( "Parameter '%s' is not already set on this request.", $key ) );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class SimpleRequest implements Request {
/**
* @var array
*/
private $params;
private $params = [];

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

/**
* @param string $action The API action.
Expand Down
2 changes: 1 addition & 1 deletion src/UsageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UsageException extends Exception {
/**
* @var array
*/
private $result;
private $result = [];

/**
* @var string
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/MediawikiApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
namespace Mediawiki\Api\Test\Integration;

use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\RsdException;
use Mediawiki\Api\SimpleRequest;
use PHPUnit\Framework\TestCase;

/**
* @author Addshore
*/
class MediawikiApiTest extends \PHPUnit\Framework\TestCase {
class MediawikiApiTest extends TestCase {

/**
* @covers Mediawiki\Api\MediawikiApi::newFromPage
*/
public function testNewFromPage() {
$api = MediawikiApi::newFromPage( TestEnvironment::newInstance()->getPageUrl() );
$this->assertInstanceOf( 'Mediawiki\Api\MediawikiApi', $api );
$this->assertInstanceOf( MediawikiApi::class, $api );
}

/**
* @covers Mediawiki\Api\MediawikiApi::newFromPage
*/
public function testNewFromPageInvalidHtml() {
$this->expectException( \Mediawiki\Api\RsdException::class );
$this->expectException( RsdException::class );
$this->expectExceptionMessageMatches( "/Unable to find RSD URL in page.*/" );
// This could be any URL that doesn't contain the RSD link, load.php works just fine!
$nonWikiPage = str_replace( 'api.php', 'load.php', TestEnvironment::newInstance()->getApiUrl() );
Expand All @@ -37,7 +39,7 @@ public function testNewFromPageInvalidHtml() {
public function testNewFromPageWithDuplicateId() {
$testPageName = __METHOD__;
$testEnv = TestEnvironment::newInstance();
$wikiPageUrl = str_replace( 'api.php', "index.php?title=$testPageName", $testEnv->getApiUrl() );
$wikiPageUrl = str_replace( 'api.php', sprintf( 'index.php?title=%s', $testPageName ), $testEnv->getApiUrl() );

// Test with no duplicate IDs.
$testEnv->savePage( $testPageName, '<p id="unique-id"></p>' );
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/TestEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct() {
}

if ( substr( $apiUrl, -7 ) !== 'api.php' ) {
$msg = "URL incorrect: $apiUrl"
$msg = sprintf( 'URL incorrect: %s', $apiUrl )
. " (Set the ADDWIKI_MW_API environment variable correctly)";
throw new Exception( $msg );
}
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/TokenHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Mediawiki\Api\Test\Integration;

use PHPUnit\Framework\TestCase;

/**
* @author Addshore
*/
class TokenHandlingTest extends \PHPUnit\Framework\TestCase {
class TokenHandlingTest extends TestCase {

/**
* @dataProvider provideTokenTypes
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/ApiUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Mediawiki\Api\Test\Unit;

use InvalidArgumentException;
use Mediawiki\Api\ApiUser;
use PHPUnit\Framework\TestCase;

/**
* @author Addshore
*
* @covers Mediawiki\Api\ApiUser
*/
class ApiUserTest extends \PHPUnit\Framework\TestCase {
class ApiUserTest extends TestCase {

/**
* @dataProvider provideValidConstruction
Expand All @@ -32,7 +34,7 @@ public function provideValidConstruction() {
* @dataProvider provideInvalidConstruction
*/
public function testInvalidConstruction( $user, $pass, $domain = null ) {
$this->expectException( 'InvalidArgumentException' );
$this->expectException( InvalidArgumentException::class );
new ApiUser( $user, $pass, $domain );
}

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/FluentRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace Mediawiki\Api\Test\Unit;

use Mediawiki\Api\FluentRequest;
use PHPUnit\Framework\TestCase;

/**
* @author Addshore
*
* @covers Mediawiki\Api\FluentRequest
*/
class FluentRequestTest extends \PHPUnit\Framework\TestCase {
class FluentRequestTest extends TestCase {

public function testFactory() {
$this->assertInstanceOf( 'Mediawiki\Api\FluentRequest', FluentRequest::factory() );
$this->assertInstanceOf( FluentRequest::class, FluentRequest::factory() );
}

public function testConstructionDefaults() {
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Guzzle/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use GuzzleHttp\HandlerStack;
use Mediawiki\Api\Guzzle\ClientFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

/**
* @author Christian Schmidt
*
* @covers Mediawiki\Api\Guzzle\ClientFactory
*/
class ClientFactoryTest extends \PHPUnit\Framework\TestCase {
class ClientFactoryTest extends TestCase {

public function testNoConfig() {
$clientFactory = new ClientFactory();
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Guzzle/MiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mediawiki\Api\Guzzle\MiddlewareFactory;
use PHPUnit\Framework\TestCase;

/**
* @author Addshore
Expand All @@ -17,7 +18,7 @@
*
* @covers Mediawiki\Api\Guzzle\MiddlewareFactory
*/
class MiddlewareFactoryTest extends \PHPUnit\Framework\TestCase {
class MiddlewareFactoryTest extends TestCase {

public function testRetriesConnectException() {
$queue = [
Expand Down Expand Up @@ -100,7 +101,7 @@ public function testRetryLimit() {
$client = $this->getClient( $queue );

$this->expectException(
'GuzzleHttp\Exception\ConnectException',
ConnectException::class,
'Error 6'
);

Expand Down
Loading

0 comments on commit 86f05f0

Please sign in to comment.