Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PHP 7.2 to 8.2 #305

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ vendor
downloads
.idea/*
tests/.phpunit.result.cache
/rector.php
/.vs
Binary file added downloads/httpful.phar
Binary file not shown.
14 changes: 7 additions & 7 deletions src/Httpful/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class Bootstrap
{

const DIR_GLUE = DIRECTORY_SEPARATOR;
const NS_GLUE = '\\';
public const DIR_GLUE = DIRECTORY_SEPARATOR;
public const NS_GLUE = '\\';

public static $registered = false;

Expand All @@ -21,7 +21,7 @@ class Bootstrap
*/
public static function init()
{
spl_autoload_register(array('\Httpful\Bootstrap', 'autoload'));
spl_autoload_register(['\\' . \Httpful\Bootstrap::class, 'autoload']);
self::registerHandlers();
}

Expand All @@ -32,15 +32,15 @@ public static function init()
*/
public static function autoload($classname)
{
self::_autoload(dirname(dirname(__FILE__)), $classname);
self::_autoload(dirname(__FILE__,2), $classname);
}

/**
* Register the autoloader and any other setup needed
*/
public static function pharInit()
{
spl_autoload_register(array('\Httpful\Bootstrap', 'pharAutoload'));
spl_autoload_register(['\\' . \Httpful\Bootstrap::class, 'pharAutoload']);
self::registerHandlers();
}

Expand Down Expand Up @@ -78,12 +78,12 @@ public static function registerHandlers()

// @todo check a conf file to load from that instead of
// hardcoding into the library?
$handlers = array(
$handlers = [
\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(),
\Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(),
\Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(),
\Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler(),
);
];

foreach ($handlers as $mime => $handler) {
// Don't overwrite if the handler has already been registered
Expand Down
6 changes: 3 additions & 3 deletions src/Httpful/Handlers/CsvHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public function parse($body)
if (empty($body))
return null;

$parsed = array();
$parsed = [];
$fp = fopen('data://text/plain;base64,' . base64_encode($body), 'r');
while (($r = fgetcsv($fp)) !== FALSE) {
$parsed[] = $r;
}

if (empty($parsed))
if ($parsed === [])
throw new \Exception("Unable to parse response as CSV");
return $parsed;
}
Expand All @@ -33,7 +33,7 @@ public function parse($body)
* @param mixed $payload
* @return string
*/
public function serialize($payload)
public function serialize($payload): string
{
$fp = fopen('php://temp/maxmemory:'. (6*1024*1024), 'r+');
$i = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Httpful/Handlers/FormHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FormHandler extends MimeHandlerAdapter
*/
public function parse($body)
{
$parsed = array();
$parsed = [];
parse_str($body, $parsed);
return $parsed;
}
Expand All @@ -23,7 +23,7 @@ public function parse($body)
* @param mixed $payload
* @return string
*/
public function serialize($payload)
public function serialize($payload): string
{
return http_build_query($payload, null, '&');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Httpful/Handlers/JsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonHandler extends MimeHandlerAdapter

public function init(array $args)
{
$this->decode_as_array = !!(array_key_exists('decode_as_array', $args) ? $args['decode_as_array'] : false);
$this->decode_as_array = (bool) ($args['decode_as_array'] ?? false);
}

/**
Expand All @@ -37,7 +37,7 @@ public function parse($body)
* @param mixed $payload
* @return string
*/
public function serialize($payload)
public function serialize($payload): string
{
return json_encode($payload);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Httpful/Handlers/MimeHandlerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MimeHandlerAdapter
{
public function __construct(array $args = array())
public function __construct(array $args = [])
{
$this->init($args);
}
Expand All @@ -36,18 +36,18 @@ public function parse($body)
* @param mixed $payload
* @return string
*/
function serialize($payload)
function serialize($payload): string
{
return (string) $payload;
}

protected function stripBom($body)
protected function stripBom($body): string
{
if ( substr($body,0,3) === "\xef\xbb\xbf" ) // UTF-8
$body = substr($body,3);
else if ( substr($body,0,4) === "\xff\xfe\x00\x00" || substr($body,0,4) === "\x00\x00\xfe\xff" ) // UTF-32
elseif ( substr($body,0,4) === "\xff\xfe\x00\x00" || substr($body,0,4) === "\x00\x00\xfe\xff" ) // UTF-32
$body = substr($body,4);
else if ( substr($body,0,2) === "\xff\xfe" || substr($body,0,2) === "\xfe\xff" ) // UTF-16
elseif ( substr($body,0,2) === "\xff\xfe" || substr($body,0,2) === "\xfe\xff" ) // UTF-16
$body = substr($body,2);
return $body;
}
Expand Down
30 changes: 15 additions & 15 deletions src/Httpful/Handlers/XmlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class XmlHandler extends MimeHandlerAdapter
/**
* @param array $conf sets configuration options
*/
public function __construct(array $conf = array())
public function __construct(array $conf = [])
{
$this->namespace = isset($conf['namespace']) ? $conf['namespace'] : '';
$this->libxml_opts = isset($conf['libxml_opts']) ? $conf['libxml_opts'] : 0;
$this->namespace = $conf['namespace'] ?? '';
$this->libxml_opts = $conf['libxml_opts'] ?? 0;
}

/**
Expand All @@ -50,9 +50,9 @@ public function parse($body)
* @return string
* @throws \Exception if unable to serialize
*/
public function serialize($payload)
public function serialize($payload): string
{
list($_, $dom) = $this->_future_serializeAsXml($payload);
[$_, $dom] = $this->_future_serializeAsXml($payload);
return $dom->saveXml();
}

Expand All @@ -61,7 +61,7 @@ public function serialize($payload)
* @return string
* @author Ted Zellers
*/
public function serialize_clean($payload)
public function serialize_clean($payload): string
{
$xml = new \XMLWriter;
$xml->openMemory();
Expand All @@ -75,7 +75,7 @@ public function serialize_clean($payload)
* @param mixed $node to serialize
* @author Ted Zellers
*/
public function serialize_node(&$xmlw, $node){
public function serialize_node(&$xmlw, $node) {
if (!is_array($node)){
$xmlw->text($node);
} else {
Expand All @@ -90,7 +90,7 @@ public function serialize_node(&$xmlw, $node){
/**
* @author Zack Douglas <zack@zackerydouglas.info>
*/
private function _future_serializeAsXml($value, $node = null, $dom = null)
private function _future_serializeAsXml($value, $node = null, $dom = null): array
{
if (!$dom) {
$dom = new \DOMDocument;
Expand All @@ -107,21 +107,21 @@ private function _future_serializeAsXml($value, $node = null, $dom = null)
$objNode = $dom->createElement(get_class($value));
$node->appendChild($objNode);
$this->_future_serializeObjectAsXml($value, $objNode, $dom);
} else if (is_array($value)) {
} elseif (is_array($value)) {
$arrNode = $dom->createElement('array');
$node->appendChild($arrNode);
$this->_future_serializeArrayAsXml($value, $arrNode, $dom);
} else if (is_bool($value)) {
} elseif (is_bool($value)) {
$node->appendChild($dom->createTextNode($value?'TRUE':'FALSE'));
} else {
$node->appendChild($dom->createTextNode($value));
}
return array($node, $dom);
return [$node, $dom];
}
/**
* @author Zack Douglas <zack@zackerydouglas.info>
*/
private function _future_serializeArrayAsXml($value, &$parent, &$dom)
private function _future_serializeArrayAsXml($value, &$parent, &$dom): array
{
foreach ($value as $k => &$v) {
$n = $k;
Expand All @@ -132,12 +132,12 @@ private function _future_serializeArrayAsXml($value, &$parent, &$dom)
$parent->appendChild($el);
$this->_future_serializeAsXml($v, $el, $dom);
}
return array($parent, $dom);
return [$parent, $dom];
}
/**
* @author Zack Douglas <zack@zackerydouglas.info>
*/
private function _future_serializeObjectAsXml($value, &$parent, &$dom)
private function _future_serializeObjectAsXml($value, &$parent, &$dom): array
{
$refl = new \ReflectionObject($value);
foreach ($refl->getProperties() as $pr) {
Expand All @@ -147,6 +147,6 @@ private function _future_serializeObjectAsXml($value, &$parent, &$dom)
$this->_future_serializeAsXml($pr->getValue($value), $el, $dom);
}
}
return array($parent, $dom);
return [$parent, $dom];
}
}
36 changes: 18 additions & 18 deletions src/Httpful/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
*/
class Http
{
const HEAD = 'HEAD';
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const PATCH = 'PATCH';
const OPTIONS = 'OPTIONS';
const TRACE = 'TRACE';
public const HEAD = 'HEAD';
public const GET = 'GET';
public const POST = 'POST';
public const PUT = 'PUT';
public const DELETE = 'DELETE';
public const PATCH = 'PATCH';
public const OPTIONS = 'OPTIONS';
public const TRACE = 'TRACE';

/**
* @return array of HTTP method strings
*/
public static function safeMethods()
public static function safeMethods(): array
{
return array(self::HEAD, self::GET, self::OPTIONS, self::TRACE);
return [self::HEAD, self::GET, self::OPTIONS, self::TRACE];
}

/**
* @param string HTTP method
* @return bool
*/
public static function isSafeMethod($method)
public static function isSafeMethod($method): bool
{
return in_array($method, self::safeMethods());
}
Expand All @@ -37,27 +37,27 @@ public static function isSafeMethod($method)
* @param string HTTP method
* @return bool
*/
public static function isUnsafeMethod($method)
public static function isUnsafeMethod($method): bool
{
return !in_array($method, self::safeMethods());
}

/**
* @return array list of (always) idempotent HTTP methods
*/
public static function idempotentMethods()
public static function idempotentMethods(): array
{
// Though it is possible to be idempotent, POST
// is not guarunteed to be, and more often than
// not, it is not.
return array(self::HEAD, self::GET, self::PUT, self::DELETE, self::OPTIONS, self::TRACE, self::PATCH);
return [self::HEAD, self::GET, self::PUT, self::DELETE, self::OPTIONS, self::TRACE, self::PATCH];
}

/**
* @param string HTTP method
* @return bool
*/
public static function isIdempotent($method)
public static function isIdempotent($method): bool
{
return in_array($method, self::safeidempotentMethodsMethods());
}
Expand All @@ -66,7 +66,7 @@ public static function isIdempotent($method)
* @param string HTTP method
* @return bool
*/
public static function isNotIdempotent($method)
public static function isNotIdempotent($method): bool
{
return !in_array($method, self::idempotentMethods());
}
Expand All @@ -78,9 +78,9 @@ public static function isNotIdempotent($method)
*
* @return array of HTTP method strings
*/
public static function canHaveBody()
public static function canHaveBody(): array
{
return array(self::POST, self::PUT, self::PATCH, self::OPTIONS);
return [self::POST, self::PUT, self::PATCH, self::OPTIONS];
}

}
6 changes: 3 additions & 3 deletions src/Httpful/Httpful.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Httpful;

class Httpful {
const VERSION = '0.3.0';
public const VERSION = '0.3.0';

private static $mimeRegistrar = array();
private static $mimeRegistrar = [];
private static $default = null;

/**
Expand Down Expand Up @@ -40,7 +40,7 @@ public static function get($mimeType = null)
* @param string $mimeType
* @return bool
*/
public static function hasParserRegistered($mimeType)
public static function hasParserRegistered($mimeType): bool
{
return isset(self::$mimeRegistrar[$mimeType]);
}
Expand Down
Loading