Skip to content

Commit

Permalink
Modernize: use the magic ::class constant
Browse files Browse the repository at this point in the history
... in all the appropriate places.

Note: in the `Requests` class where it would result in a "self" reference, I've chosen to use `static` to allow for child classes overloading the referenced methods.
  • Loading branch information
jrfnl committed Jul 30, 2021
1 parent 6c5f62e commit 8da9ba9
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 58 deletions.
14 changes: 7 additions & 7 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function autoloader($class) {
* @codeCoverageIgnore
*/
public static function register_autoloader() {
spl_autoload_register(array('Requests', 'autoloader'));
spl_autoload_register(array(static::class, 'autoloader'));
}

/**
Expand All @@ -164,8 +164,8 @@ public static function register_autoloader() {
public static function add_transport($transport) {
if (empty(self::$transports)) {
self::$transports = array(
'Requests_Transport_cURL',
'Requests_Transport_fsockopen',
Requests_Transport_cURL::class,
Requests_Transport_fsockopen::class,
);
}

Expand Down Expand Up @@ -194,8 +194,8 @@ protected static function get_transport($capabilities = array()) {

if (empty(self::$transports)) {
self::$transports = array(
'Requests_Transport_cURL',
'Requests_Transport_fsockopen',
Requests_Transport_cURL::class,
Requests_Transport_fsockopen::class,
);
}

Expand Down Expand Up @@ -430,7 +430,7 @@ public static function request_multiple($requests, $options = array()) {
$options = array_merge(self::get_default_options(true), $options);

if (!empty($options['hooks'])) {
$options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
$options['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple'));
if (!empty($options['complete'])) {
$options['hooks']->register('multiple.request.complete', $options['complete']);
}
Expand Down Expand Up @@ -461,7 +461,7 @@ public static function request_multiple($requests, $options = array()) {

// Ensure we only hook in once
if ($request['options']['hooks'] !== $options['hooks']) {
$request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));
$request['options']['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple'));
if (!empty($request['options']['complete'])) {
$request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']);
}
Expand Down
4 changes: 2 additions & 2 deletions library/Requests/Exception/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function getReason() {
*/
public static function get_class($code) {
if (!$code) {
return 'Requests_Exception_HTTP_Unknown';
return Requests_Exception_HTTP_Unknown::class;
}

$class = sprintf('Requests_Exception_HTTP_%d', $code);
if (class_exists($class)) {
return $class;
}

return 'Requests_Exception_HTTP_Unknown';
return Requests_Exception_HTTP_Unknown::class;
}
}
9 changes: 6 additions & 3 deletions tests/Auth/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Requests;
use Requests\Tests\TestCase;
use Requests_Auth_Basic;
use Requests_Exception;
use Requests_Transport_cURL;
use Requests_Transport_fsockopen;

class BasicTest extends TestCase {
public static function transportProvider() {
return array(
array('Requests_Transport_fsockopen'),
array('Requests_Transport_cURL'),
array(Requests_Transport_fsockopen::class),
array(Requests_Transport_cURL::class),
);
}

Expand Down Expand Up @@ -83,7 +86,7 @@ public function testPOSTUsingInstantiation($transport) {
}

public function testMissingPassword() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid number of arguments');
new Requests_Auth_Basic(array('user'));
}
Expand Down
17 changes: 9 additions & 8 deletions tests/IDNAEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Requests\Tests;

use Requests\Tests\TestCase;
use Requests_Exception;
use Requests_IDNAEncoder;

class IDNAEncoderTest extends TestCase {
Expand All @@ -28,21 +29,21 @@ public function testEncoding($data, $expected) {
}

public function testASCIITooLong() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Provided string is too long');
$data = str_repeat('abcd', 20);
Requests_IDNAEncoder::encode($data);
}

public function testEncodedTooLong() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Encoded string is too long');
$data = str_repeat("\xe4\xbb\x96", 60);
Requests_IDNAEncoder::encode($data);
}

public function testAlreadyPrefixed() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Provided string begins with ACE prefix');
Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96");
}
Expand All @@ -68,31 +69,31 @@ public function testFourByteCharacter() {
}

public function testFiveByteCharacter() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6");
}

public function testSixByteCharacter() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6");
}

public function testInvalidASCIICharacterWithMultibyte() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\0\xc2\xb6");
}

public function testUnfinishedMultibyte() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xc2");
}

public function testPartialMultibyte() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xc2\xc2\xb6");
}
Expand Down
13 changes: 8 additions & 5 deletions tests/Proxy/HTTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Requests;
use Requests\Tests\TestCase;
use Requests_Exception;
use Requests_Proxy_HTTP;
use Requests_Transport_cURL;
use Requests_Transport_fsockopen;

class HTTPTest extends TestCase {
protected function checkProxyAvailable($type = '') {
Expand All @@ -25,8 +28,8 @@ protected function checkProxyAvailable($type = '') {

public function transportProvider() {
return array(
array('Requests_Transport_cURL'),
array('Requests_Transport_fsockopen'),
array(Requests_Transport_cURL::class),
array(Requests_Transport_fsockopen::class),
);
}

Expand Down Expand Up @@ -74,7 +77,7 @@ public function testConnectInvalidParameters($transport) {
'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'),
'transport' => $transport,
);
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Invalid number of arguments');
Requests::get(httpbin('/get'), array(), $options);
}
Expand Down Expand Up @@ -134,10 +137,10 @@ public function testConnectWithInvalidAuth($transport) {
);

if (version_compare(phpversion(), '5.5.0', '>=') === true
&& $transport === 'Requests_Transport_fsockopen'
&& $transport === Requests_Transport_fsockopen::class
) {
// @TODO fsockopen connection times out on invalid auth instead of returning 407.
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('fsocket timed out');
}

Expand Down
11 changes: 6 additions & 5 deletions tests/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Requests\Tests\Mock\RawTransportMock;
use Requests\Tests\Mock\TransportMock;
use Requests\Tests\TestCase;
use Requests_Exception;
use Requests_Response_Headers;

class RequestsTest extends TestCase {

public function testInvalidProtocol() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Only HTTP(S) requests are handled');
Requests::request('ftp://128.0.0.1/');
}
Expand Down Expand Up @@ -116,7 +117,7 @@ public function testInvalidProtocolVersion() {
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', array(), $options);
}
Expand All @@ -132,7 +133,7 @@ public function testSingleCRLFSeparator() {
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Missing header/body separator');
Requests::get('http://example.com/', array(), $options);
}
Expand All @@ -145,7 +146,7 @@ public function testInvalidStatus() {
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', array(), $options);
}
Expand All @@ -164,7 +165,7 @@ public function test30xWithoutLocation() {

public function testTimeoutException() {
$options = array('timeout' => 0.5);
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('timed out');
Requests::get(httpbin('/delay/3'), array(), $options);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Response/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Requests\Tests\Response;

use Requests\Tests\TestCase;
use Requests_Exception;
use Requests_Response_Headers;

class HeadersTest extends TestCase {
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testIteration() {
}

public function testInvalidKey() {
$this->expectException('Requests_Exception');
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Object is a dictionary, not a list');
$headers = new Requests_Response_Headers();
$headers[] = 'text/plain';
Expand Down
5 changes: 3 additions & 2 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Requests\Tests;

use Requests\Tests\TestCase;
use Requests_Response;
use Requests_Session;

class SessionTest extends TestCase {
Expand Down Expand Up @@ -138,7 +139,7 @@ public function testMultiple() {

// test1
$this->assertNotEmpty($responses['test1']);
$this->assertInstanceOf('Requests_Response', $responses['test1']);
$this->assertInstanceOf(Requests_Response::class, $responses['test1']);
$this->assertSame(200, $responses['test1']->status_code);

$result = json_decode($responses['test1']->body, true);
Expand All @@ -147,7 +148,7 @@ public function testMultiple() {

// test2
$this->assertNotEmpty($responses['test2']);
$this->assertInstanceOf('Requests_Response', $responses['test2']);
$this->assertInstanceOf(Requests_Response::class, $responses['test2']);
$this->assertSame(200, $responses['test2']->status_code);

$result = json_decode($responses['test2']->body, true);
Expand Down
Loading

0 comments on commit 8da9ba9

Please sign in to comment.