diff --git a/composer.json b/composer.json index 02da7399..6bc27531 100644 --- a/composer.json +++ b/composer.json @@ -55,7 +55,7 @@ }, "require": { "php": ">=5.4.0", - "bdk/http-message": "^1.3.2 || ^2.3.2 || ^3.3.2", + "bdk/http-message": "^1.3.3 || ^2.3.3 || ^3.3.3", "jdorn/sql-formatter": "^1.2" }, "require-dev": { diff --git a/src/CurlHttpMessage/Exception/RequestException.php b/src/CurlHttpMessage/Exception/RequestException.php index a5d5fcf5..bb2fdc7e 100644 --- a/src/CurlHttpMessage/Exception/RequestException.php +++ b/src/CurlHttpMessage/Exception/RequestException.php @@ -30,8 +30,8 @@ class RequestException extends RuntimeException */ public function __construct($message, RequestInterface $request, $response = null, $prevException = null) { - \bdk\Debug\Utility\Php::assertType($response, 'Psr\Http\Message\ResponseInterface'); - \bdk\Debug\Utility\Php::assertType($prevException, 'Exception'); + \bdk\Debug\Utility::assertType($response, 'Psr\Http\Message\ResponseInterface'); + \bdk\Debug\Utility::assertType($prevException, 'Exception'); $this->request = $request; $this->response = $response; @@ -51,8 +51,8 @@ public function __construct($message, RequestInterface $request, $response = nul */ public static function create(RequestInterface $request, $response = null, $prevException = null) { - \bdk\Debug\Utility\Php::assertType($response, 'Psr\Http\Message\ResponseInterface'); - \bdk\Debug\Utility\Php::assertType($prevException, 'Exception'); + \bdk\Debug\Utility::assertType($response, 'Psr\Http\Message\ResponseInterface'); + \bdk\Debug\Utility::assertType($prevException, 'Exception'); $level = $response ? (int) \floor($response->getStatusCode() / 100) @@ -97,7 +97,7 @@ public function getResponse() */ private static function buildMessage(RequestInterface $request, $response = null) { - \bdk\Debug\Utility\Php::assertType($response, 'Psr\Http\Message\ResponseInterface'); + \bdk\Debug\Utility::assertType($response, 'Psr\Http\Message\ResponseInterface'); if (!$response) { $label = 'Error completing request'; diff --git a/src/CurlHttpMessage/Factory.php b/src/CurlHttpMessage/Factory.php index dd8d34f2..2ed96172 100644 --- a/src/CurlHttpMessage/Factory.php +++ b/src/CurlHttpMessage/Factory.php @@ -128,7 +128,7 @@ public function buildResponse($code = 200, $reasonPhrase = '', $headers = array( */ public static function buildStack($handler = null) { - \bdk\Debug\Utility\Php::assertType($handler, 'callable'); + \bdk\Debug\Utility::assertType($handler, 'callable'); if ($handler === null) { $syncHandler = new Curl(); $asyncHandler = new CurlMulti(); diff --git a/src/CurlHttpMessage/Handler/Mock.php b/src/CurlHttpMessage/Handler/Mock.php index cfbd8e9e..8eaecc47 100644 --- a/src/CurlHttpMessage/Handler/Mock.php +++ b/src/CurlHttpMessage/Handler/Mock.php @@ -39,8 +39,8 @@ class Mock implements Countable */ public function __construct(array $queue = array(), $onFulfilled = null, $onRejected = null) { - \bdk\Debug\Utility\Php::assertType($onFulfilled, 'callable'); - \bdk\Debug\Utility\Php::assertType($onRejected, 'callable'); + \bdk\Debug\Utility::assertType($onFulfilled, 'callable'); + \bdk\Debug\Utility::assertType($onRejected, 'callable'); $this->onFulfilled = $onFulfilled; $this->onRejected = $onRejected; diff --git a/src/Debug/AbstractDebug.php b/src/Debug/AbstractDebug.php index e81aa7a6..3456b4b3 100644 --- a/src/Debug/AbstractDebug.php +++ b/src/Debug/AbstractDebug.php @@ -243,7 +243,7 @@ public function onCfgServiceProvider($val) */ public function publishBubbleEvent($eventName, Event $event, $debug = null) { - $this->php->assertType($debug, 'bdk\Debug'); + $this->utility->assertType($debug, 'bdk\Debug'); if ($debug === null) { $subject = $event->getSubject(); /** @var Debug */ diff --git a/src/Debug/Abstraction/Object/Helper.php b/src/Debug/Abstraction/Object/Helper.php index a9cd2afb..6f5469c6 100644 --- a/src/Debug/Abstraction/Object/Helper.php +++ b/src/Debug/Abstraction/Object/Helper.php @@ -15,6 +15,7 @@ use bdk\Debug\Abstraction\Abstraction; use bdk\Debug\Abstraction\AbstractObject; use bdk\Debug\Abstraction\Object\Abstraction as ObjectAbstraction; +use bdk\Debug\Utility; use bdk\Debug\Utility\Php as PhpUtil; use bdk\Debug\Utility\PhpDoc; use ReflectionAttribute; @@ -216,7 +217,7 @@ public static function getType($phpDocType, Reflector $reflector) */ protected static function getTypeString($type = null) { - PhpUtil::assertType($type, 'ReflectionType'); + Utility::assertType($type, 'ReflectionType'); if ($type === null) { return null; } diff --git a/src/Debug/Collector/AbstractAsyncMiddleware.php b/src/Debug/Collector/AbstractAsyncMiddleware.php index e7cf8f14..c46d5048 100644 --- a/src/Debug/Collector/AbstractAsyncMiddleware.php +++ b/src/Debug/Collector/AbstractAsyncMiddleware.php @@ -15,6 +15,8 @@ use bdk\Debug; use bdk\Debug\AbstractComponent; use bdk\Debug\LogEntry; +use bdk\Debug\Utility; +use bdk\HttpMessage\Utility\Stream as StreamUtility; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -58,7 +60,7 @@ class AbstractAsyncMiddleware extends AbstractComponent */ public function __construct($cfg = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + Utility::assertType($debug, 'bdk\Debug'); $this->setCfg($cfg); if (!$debug) { $debug = Debug::getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon'])); @@ -213,7 +215,7 @@ protected function getBody(MessageInterface $msg) $contentType = $contentType ? $contentType[0] : null; - $body = $this->debug->utility->getStreamContents($bodyStream); + $body = StreamUtility::getContents($bodyStream); if (\strlen($body) === 0) { return ''; } @@ -287,8 +289,8 @@ protected function logRequestBody(RequestInterface $request) */ protected function logResponse($response = null, array $requestInfo = array(), $rejectReason = null) { - \bdk\Debug\Utility\Php::assertType($response, 'Psr\Http\Message\ResponseInterface'); - \bdk\Debug\Utility\Php::assertType($rejectReason, 'Exception'); + Utility::assertType($response, 'Psr\Http\Message\ResponseInterface'); + Utility::assertType($rejectReason, 'Exception'); $duration = $this->debug->timeEnd($this->cfg['label'] . ':' . $requestInfo['requestId'], false); $metaAppend = $requestInfo['isAsynchronous'] && $this->cfg['asyncResponseWithRequest'] diff --git a/src/Debug/Collector/CurlHttpMessageMiddleware.php b/src/Debug/Collector/CurlHttpMessageMiddleware.php index 99a44ff0..11435999 100644 --- a/src/Debug/Collector/CurlHttpMessageMiddleware.php +++ b/src/Debug/Collector/CurlHttpMessageMiddleware.php @@ -27,7 +27,7 @@ class CurlHttpMessageMiddleware extends AbstractAsyncMiddleware */ public function __construct($cfg = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->cfg = \array_merge($this->cfg, array( 'idPrefix' => 'curl_', diff --git a/src/Debug/Collector/DoctrineLogger.php b/src/Debug/Collector/DoctrineLogger.php index 58e4a027..3f3066db 100644 --- a/src/Debug/Collector/DoctrineLogger.php +++ b/src/Debug/Collector/DoctrineLogger.php @@ -52,8 +52,8 @@ class DoctrineLogger implements SQLLoggerInterface */ public function __construct($connection = null, $debug = null) { - \bdk\Debug\Utility\Php::assertType($connection, 'Doctrine\DBAL\Connection'); - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($connection, 'Doctrine\DBAL\Connection'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('Doctrine', array('channelIcon' => $this->icon)); diff --git a/src/Debug/Collector/GuzzleMiddleware.php b/src/Debug/Collector/GuzzleMiddleware.php index a3c7bf07..9aa3811d 100644 --- a/src/Debug/Collector/GuzzleMiddleware.php +++ b/src/Debug/Collector/GuzzleMiddleware.php @@ -30,7 +30,7 @@ class GuzzleMiddleware extends AbstractAsyncMiddleware */ public function __construct($cfg = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->cfg = \array_merge($this->cfg, array( 'idPrefix' => 'guzzle_', diff --git a/src/Debug/Collector/MySqli.php b/src/Debug/Collector/MySqli.php index 4b23c0b9..f1f9491e 100644 --- a/src/Debug/Collector/MySqli.php +++ b/src/Debug/Collector/MySqli.php @@ -61,7 +61,7 @@ class MySqli extends mysqliBase */ public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null, $debug = null) // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->doConstruct(\func_num_args() ? \array_slice(\func_get_args(), 0, 6) diff --git a/src/Debug/Collector/Pdo.php b/src/Debug/Collector/Pdo.php index 7ac5be44..fe5e0fcc 100644 --- a/src/Debug/Collector/Pdo.php +++ b/src/Debug/Collector/Pdo.php @@ -46,7 +46,7 @@ class Pdo extends PdoBase */ public function __construct(PdoBase $pdo, $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('PDO', array('channelIcon' => $this->icon)); diff --git a/src/Debug/Collector/PhpCurlClass.php b/src/Debug/Collector/PhpCurlClass.php index 3b4b66dd..cabc04aa 100644 --- a/src/Debug/Collector/PhpCurlClass.php +++ b/src/Debug/Collector/PhpCurlClass.php @@ -61,7 +61,7 @@ class PhpCurlClass extends Curl */ public function __construct($options = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->debugOptions = \array_merge($this->debugOptions, $options); if (!$debug) { diff --git a/src/Debug/Collector/SimpleCache.php b/src/Debug/Collector/SimpleCache.php index 3a650cbf..8ff1e649 100644 --- a/src/Debug/Collector/SimpleCache.php +++ b/src/Debug/Collector/SimpleCache.php @@ -53,7 +53,7 @@ class SimpleCache implements CacheInterface */ public function __construct(CacheInterface $cache, $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('SimpleCache', array('channelIcon' => $this->icon)); diff --git a/src/Debug/Collector/SimpleCache/CallInfo.php b/src/Debug/Collector/SimpleCache/CallInfo.php index 5b27bf99..1cd4fd67 100644 --- a/src/Debug/Collector/SimpleCache/CallInfo.php +++ b/src/Debug/Collector/SimpleCache/CallInfo.php @@ -13,7 +13,7 @@ namespace bdk\Debug\Collector\SimpleCache; use bdk\Debug\AbstractComponent; -use bdk\Debug\Utility\Php as PhpUtil; +use bdk\Debug\Utility; use Exception; /** @@ -99,7 +99,7 @@ public function __debugInfo() */ public function end($exception = null) { - PhpUtil::assertType($exception, 'Exception'); + Utility::assertType($exception, 'Exception'); $this->exception = $exception; $this->timeEnd = \microtime(true); diff --git a/src/Debug/Collector/SoapClient.php b/src/Debug/Collector/SoapClient.php index ed84f10c..9a4ef0dd 100644 --- a/src/Debug/Collector/SoapClient.php +++ b/src/Debug/Collector/SoapClient.php @@ -56,7 +56,7 @@ class SoapClient extends SoapClientBase */ public function __construct($wsdl, $options = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('Soap', array('channelIcon' => $this->icon)); @@ -269,7 +269,7 @@ private function isViaCall() */ private function logConstruct($wsdl, $options, $exception = null) { - \bdk\Debug\Utility\Php::assertType($exception, 'Exception'); + \bdk\Debug\Utility::assertType($exception, 'Exception'); $this->debug->groupCollapsed('SoapClient::__construct', $wsdl ?: 'non-WSDL mode', $this->debug->meta('icon', $this->icon)); if ($wsdl && !empty($options['list_functions'])) { @@ -305,7 +305,7 @@ private function logConstruct($wsdl, $options, $exception = null) */ private function logReqRes($action, $exception = null, $logParsedFault = false) { - \bdk\Debug\Utility\Php::assertType($exception, 'Exception'); + \bdk\Debug\Utility::assertType($exception, 'Exception'); $fault = null; $xmlRequest = $this->debugGetXmlRequest($action); diff --git a/src/Debug/Collector/StatementInfo.php b/src/Debug/Collector/StatementInfo.php index 6a454e3e..8b39633b 100644 --- a/src/Debug/Collector/StatementInfo.php +++ b/src/Debug/Collector/StatementInfo.php @@ -102,8 +102,8 @@ class StatementInfo extends AbstractComponent */ public function __construct($sql, $params = array(), $types = array()) { - \bdk\Debug\Utility\Php::assertType($params, 'array'); - \bdk\Debug\Utility\Php::assertType($types, 'array'); + \bdk\Debug\Utility::assertType($params, 'array'); + \bdk\Debug\Utility::assertType($types, 'array'); $this->memoryStart = \memory_get_usage(false); $this->params = $params ?: array(); @@ -180,7 +180,7 @@ public function appendLog(Debug $debug, array $metaOverride = array()) */ public function end($exception = null, $rowCount = null) { - \bdk\Debug\Utility\Php::assertType($exception, 'Exception'); + \bdk\Debug\Utility::assertType($exception, 'Exception'); $this->exception = $exception; $this->rowCount = $rowCount; diff --git a/src/Debug/Collector/SwiftMailerLogger.php b/src/Debug/Collector/SwiftMailerLogger.php index a7c48987..8decba76 100644 --- a/src/Debug/Collector/SwiftMailerLogger.php +++ b/src/Debug/Collector/SwiftMailerLogger.php @@ -68,7 +68,7 @@ class SwiftMailerLogger implements */ public function __construct($debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('SwiftMailer', array('channelIcon' => $this->icon)); diff --git a/src/Debug/Collector/TwigExtension.php b/src/Debug/Collector/TwigExtension.php index 5a168777..73928506 100644 --- a/src/Debug/Collector/TwigExtension.php +++ b/src/Debug/Collector/TwigExtension.php @@ -37,8 +37,8 @@ class TwigExtension extends ProfilerExtension */ public function __construct($debug = null, $profile = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); - \bdk\Debug\Utility\Php::assertType($profile, 'Twig\Profiler\Profile'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($profile, 'Twig\Profiler\Profile'); if (!$debug) { $debug = Debug::getChannel('Twig', array('channelIcon' => $this->icon)); diff --git a/src/Debug/Dump/AbstractValue.php b/src/Debug/Dump/AbstractValue.php index f643a178..61d0cf82 100644 --- a/src/Debug/Dump/AbstractValue.php +++ b/src/Debug/Dump/AbstractValue.php @@ -82,7 +82,7 @@ public function __construct(Dumper $dumper) */ public function checkTimestamp($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if ($abs && $abs['typeMore'] === Type::TYPE_TIMESTAMP) { $datetime = new DateTime('@' . (int) $val); diff --git a/src/Debug/Dump/Base/Value.php b/src/Debug/Dump/Base/Value.php index 8b3d5876..35c8b1b3 100644 --- a/src/Debug/Dump/Base/Value.php +++ b/src/Debug/Dump/Base/Value.php @@ -48,7 +48,7 @@ public function markupIdentifier($val) */ protected function dumpArray(array $array, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if ($this->optionGet('isMaxDepth')) { return 'array *MAX DEPTH*'; @@ -93,7 +93,7 @@ protected function dumpCallable(Abstraction $abs) */ protected function dumpFloat($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $date = $this->checkTimestamp($val, $abs); return $date @@ -118,7 +118,7 @@ protected function dumpIdentifier(Abstraction $abs) */ protected function dumpInt($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $val = $this->dumpFloat($val, $abs); return \is_string($val) @@ -183,7 +183,7 @@ protected function dumpResource(Abstraction $abs) */ protected function dumpString($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if (\is_numeric($val)) { $date = $this->checkTimestamp($val, $abs); diff --git a/src/Debug/Dump/Html/HtmlArray.php b/src/Debug/Dump/Html/HtmlArray.php index f0f96f7b..b77e5777 100644 --- a/src/Debug/Dump/Html/HtmlArray.php +++ b/src/Debug/Dump/Html/HtmlArray.php @@ -52,7 +52,7 @@ public function __construct(ValDumper $valDumper) */ public function dump(array $array, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $opts = $this->optionsGet(); if ($opts['isMaxDepth']) { diff --git a/src/Debug/Dump/Html/HtmlString.php b/src/Debug/Dump/Html/HtmlString.php index 44a55dd2..6fb592c7 100644 --- a/src/Debug/Dump/Html/HtmlString.php +++ b/src/Debug/Dump/Html/HtmlString.php @@ -89,7 +89,7 @@ public function __get($property) */ public function dump($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if (\is_numeric($val)) { $this->valDumper->checkTimestamp($val, $abs); diff --git a/src/Debug/Dump/Html/Value.php b/src/Debug/Dump/Html/Value.php index f3ba9674..d730bd95 100644 --- a/src/Debug/Dump/Html/Value.php +++ b/src/Debug/Dump/Html/Value.php @@ -70,7 +70,7 @@ public function __construct(Dumper $dumper) */ public function checkTimestamp($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $date = parent::checkTimestamp($val, $abs); if ($date) { @@ -225,7 +225,7 @@ protected function dumpCallable(Abstraction $abs) */ protected function dumpFloat($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if ($val === Type::TYPE_FLOAT_INF) { return 'INF'; @@ -295,7 +295,7 @@ protected function dumpRecursion() */ protected function dumpString($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); return $this->string->dump($val, $abs); } diff --git a/src/Debug/Dump/Text/Value.php b/src/Debug/Dump/Text/Value.php index d905a0f5..f4efefd4 100644 --- a/src/Debug/Dump/Text/Value.php +++ b/src/Debug/Dump/Text/Value.php @@ -82,7 +82,7 @@ protected function addQuotes($val) */ protected function dumpArray(array $array, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $isNested = $this->valDepth > 0; $this->valDepth++; @@ -120,7 +120,7 @@ protected function dumpBool($val) */ protected function dumpFloat($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if ($val === Type::TYPE_FLOAT_INF) { return 'INF'; @@ -166,7 +166,7 @@ protected function dumpObject(ObjectAbstraction $abs) */ protected function dumpString($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $date = \is_numeric($val) ? $this->checkTimestamp($val, $abs) diff --git a/src/Debug/Dump/TextAnsi/Value.php b/src/Debug/Dump/TextAnsi/Value.php index c6b3b946..88a7aed0 100644 --- a/src/Debug/Dump/TextAnsi/Value.php +++ b/src/Debug/Dump/TextAnsi/Value.php @@ -115,7 +115,7 @@ protected function addQuotes($val) */ protected function dumpArray(array $array, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $this->valDepth++; $isNested = $this->valDepth > 0; @@ -193,7 +193,7 @@ protected function dumpBool($val) */ protected function dumpFloat($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if ($val === Type::TYPE_FLOAT_INF) { $val = 'INF'; @@ -263,7 +263,7 @@ protected function dumpRecursion() */ protected function dumpString($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); if (\is_numeric($val)) { return $this->dumpStringNumeric($val, $abs); @@ -337,7 +337,7 @@ protected function dumpStringBinary(Abstraction $abs) */ private function dumpStringNumeric($val, $abs = null) { - \bdk\Debug\Utility\Php::assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); + $this->debug->utility->assertType($abs, 'bdk\Debug\Abstraction\Abstraction'); $escapeCodes = $this->cfg['escapeCodes']; $date = $this->checkTimestamp($val, $abs); diff --git a/src/Debug/Framework/Laravel/CacheEventsSubscriber.php b/src/Debug/Framework/Laravel/CacheEventsSubscriber.php index f5d6f9a2..501ced1a 100644 --- a/src/Debug/Framework/Laravel/CacheEventsSubscriber.php +++ b/src/Debug/Framework/Laravel/CacheEventsSubscriber.php @@ -54,7 +54,7 @@ class CacheEventsSubscriber */ public function __construct($options = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->options = \array_merge($this->options, $options); $channelOptions = array( diff --git a/src/Debug/Framework/Laravel/EventsSubscriber.php b/src/Debug/Framework/Laravel/EventsSubscriber.php index 28b6fa59..473620b6 100644 --- a/src/Debug/Framework/Laravel/EventsSubscriber.php +++ b/src/Debug/Framework/Laravel/EventsSubscriber.php @@ -41,7 +41,7 @@ class EventsSubscriber */ public function __construct($debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $channelOptions = array( 'channelIcon' => $this->icon, diff --git a/src/Debug/Framework/Laravel/Middleware.php b/src/Debug/Framework/Laravel/Middleware.php index 36b3b428..d9048140 100644 --- a/src/Debug/Framework/Laravel/Middleware.php +++ b/src/Debug/Framework/Laravel/Middleware.php @@ -106,7 +106,7 @@ public function handle(Request $request, Closure $next) */ protected function getUserInformation($user = null) { - \bdk\Debug\Utility\Php::assertType($user, 'Illuminate\Foundation\Auth\User'); + $this->debug->utility->assertType($user, 'Illuminate\Foundation\Auth\User'); // Defaults if ($user === null) { diff --git a/src/Debug/Framework/Slim2.php b/src/Debug/Framework/Slim2.php index 5b5e11c6..af824607 100644 --- a/src/Debug/Framework/Slim2.php +++ b/src/Debug/Framework/Slim2.php @@ -40,8 +40,8 @@ class Slim2 */ public function __construct($debug = null, $prevWriter = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); - \bdk\Debug\Utility\Php::assertType($prevWriter, 'object'); // object not avail as type-hint until php 7.2 + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($prevWriter, 'object'); // object not avail as type-hint until php 7.2 if (!$debug) { $debug = Debug::getChannel('Slim'); diff --git a/src/Debug/Framework/Yii1_1/LogRoute.php b/src/Debug/Framework/Yii1_1/LogRoute.php index 9f2c34ac..ea4b9c50 100644 --- a/src/Debug/Framework/Yii1_1/LogRoute.php +++ b/src/Debug/Framework/Yii1_1/LogRoute.php @@ -68,7 +68,7 @@ class LogRoute extends CLogRoute */ public function __construct($debug = null, $opts = array()) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('Yii'); diff --git a/src/Debug/Framework/Yii1_1/PdoCollector.php b/src/Debug/Framework/Yii1_1/PdoCollector.php index 87990021..b2d554ad 100644 --- a/src/Debug/Framework/Yii1_1/PdoCollector.php +++ b/src/Debug/Framework/Yii1_1/PdoCollector.php @@ -46,7 +46,7 @@ public function __construct(CApplicationComponent $component) */ public function collect($dbConnection = null) { - \bdk\Debug\Utility\Php::assertType($dbConnection, 'CDbConnection'); + \bdk\Debug\Utility::assertType($dbConnection, 'CDbConnection'); if ($this->component->shouldCollect('pdo') === false) { return; diff --git a/src/Debug/Framework/Yii2/LogTarget.php b/src/Debug/Framework/Yii2/LogTarget.php index bc9ee174..570d8912 100644 --- a/src/Debug/Framework/Yii2/LogTarget.php +++ b/src/Debug/Framework/Yii2/LogTarget.php @@ -51,7 +51,7 @@ class LogTarget extends Target */ public function __construct($debug = null, $config = array()) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getChannel('Yii'); diff --git a/src/Debug/Plugin/LogPhp.php b/src/Debug/Plugin/LogPhp.php index 41cde2ef..d2d79b4d 100644 --- a/src/Debug/Plugin/LogPhp.php +++ b/src/Debug/Plugin/LogPhp.php @@ -50,7 +50,9 @@ class LogPhp implements SubscriberInterface */ public function __construct() { - $this->iniValues = array('dateTimezone' => \ini_get('date.timezone')); + $this->iniValues = array( + 'dateTimezone' => \ini_get('date.timezone'), + ); } /** @@ -58,7 +60,9 @@ public function __construct() */ public function getSubscriptions() { - return array(Debug::EVENT_BOOTSTRAP => 'onBootstrap'); + return array( + Debug::EVENT_BOOTSTRAP => 'onBootstrap', + ); } /** @@ -125,7 +129,7 @@ protected function logPhpEr() */ protected function logPhpVersion() { - $buildDate = $this->phpBuildDate(); + $buildDate = $this->debug->php->buildDate(); $this->debug->log('PHP Version', PHP_VERSION); $this->debug->log('Server API', PHP_SAPI); if ($buildDate) { @@ -360,21 +364,4 @@ private function logXdebug() } $this->debug->log($msg); } - - /** - * Return the build date of the PHP binary - * - * @return string|null - */ - protected function phpBuildDate() - { - \ob_start(); - \phpinfo(INFO_GENERAL); - $phpInfo = \ob_get_clean(); - $phpInfo = \strip_tags($phpInfo); - \preg_match('/Build Date (?:=> )?([^\n]*)/', $phpInfo, $matches); - return $matches - ? $matches[1] - : null; - } } diff --git a/src/Debug/Plugin/LogRequest.php b/src/Debug/Plugin/LogRequest.php index 1d745117..1d308f46 100644 --- a/src/Debug/Plugin/LogRequest.php +++ b/src/Debug/Plugin/LogRequest.php @@ -14,6 +14,7 @@ use bdk\Debug; use bdk\HttpMessage\Utility\ContentType; +use bdk\HttpMessage\Utility\Stream as StreamUtility; use bdk\PubSub\Event; use bdk\PubSub\SubscriberInterface; @@ -131,7 +132,7 @@ private function logFiles() private function logInput($method, $contentType) { $request = $this->debug->serverRequest; - $input = $this->debug->utility->getStreamContents($request->getBody()); + $input = StreamUtility::getContents($request->getBody()); $methodExpectBody = $this->debug->utility->httpMethodHasBody($method); $logInput = $input || $methodExpectBody @@ -170,7 +171,7 @@ private function logPostOrInput() $method = $request->getMethod(); $mediaTypeUser = (string) $request->getMediaType(); $mediaType = $this->detectContentType( - $this->debug->utility->getStreamContents($request->getBody()), + StreamUtility::getContents($request->getBody()), $mediaTypeUser ); $parsedBody = $request->getParsedBody(); diff --git a/src/Debug/Plugin/LogResponse.php b/src/Debug/Plugin/LogResponse.php index 475c39f5..8945665d 100644 --- a/src/Debug/Plugin/LogResponse.php +++ b/src/Debug/Plugin/LogResponse.php @@ -13,6 +13,7 @@ namespace bdk\Debug\Plugin; use bdk\Debug; +use bdk\HttpMessage\Utility\Stream as StreamUtility; use bdk\PubSub\Event; use bdk\PubSub\SubscriberInterface; @@ -126,7 +127,7 @@ private function getResponseInfo() $contentLength = \strlen($content); if ($this->debug->response) { $content = $this->debug->response->getBody(); - $contentLength = $content->getSize() ?: \strlen($this->debug->utility->getStreamContents($content)); + $contentLength = $content->getSize() ?: \strlen(StreamUtility::getContents($content)); } return array( 'content' => $content, diff --git a/src/Debug/Psr15/Middleware.php b/src/Debug/Psr15/Middleware.php index 46699ec7..5b755aad 100644 --- a/src/Debug/Psr15/Middleware.php +++ b/src/Debug/Psr15/Middleware.php @@ -40,7 +40,7 @@ class Middleware extends AbstractComponent implements MiddlewareInterface */ public function __construct($debug = null, $cfg = array()) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->debug = $debug ?: Debug::getInstance(); $this->cfg = \array_merge(array( diff --git a/src/Debug/Psr3/Logger.php b/src/Debug/Psr3/Logger.php index 8ea5f70b..8c0d7314 100644 --- a/src/Debug/Psr3/Logger.php +++ b/src/Debug/Psr3/Logger.php @@ -54,7 +54,7 @@ class Logger extends AbstractLogger */ public function __construct($debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = Debug::getInstance(); diff --git a/src/Debug/Route/AbstractRoute.php b/src/Debug/Route/AbstractRoute.php index efbd3039..3485976e 100644 --- a/src/Debug/Route/AbstractRoute.php +++ b/src/Debug/Route/AbstractRoute.php @@ -153,7 +153,7 @@ public function processSummary() */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $this->dumper->crateRaw = false; $this->data = $this->debug->data->get(); diff --git a/src/Debug/Route/ChromeLogger.php b/src/Debug/Route/ChromeLogger.php index 6100a533..e2c728ca 100644 --- a/src/Debug/Route/ChromeLogger.php +++ b/src/Debug/Route/ChromeLogger.php @@ -60,6 +60,12 @@ class ChromeLogger extends AbstractRoute 'warn', ]; + /** @var int Current group depth */ + protected $depth = 0; + + /** @var bool Whether we're only collecting groups due to header size limit */ + protected $groupOnly = false; + /** * @var array header data */ @@ -70,6 +76,9 @@ class ChromeLogger extends AbstractRoute 'rows' => [], ); + /** @var int Maximum header length */ + protected $max = 0; + /** * Constructor * @@ -90,15 +99,16 @@ public function __construct(Debug $debug) */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $this->dumper->crateRaw = false; $this->data = $this->debug->data->get(); + $this->data['log'] = \array_values($this->data['log']); $this->buildJsonData(); - $max = $this->getMaxLength(); + $this->max = $this->getMaxLength(); $encoded = $this->encode($this->jsonData); - if ($max && \strlen($encoded) > $max) { - $this->reduceData($max); + if ($this->max && \strlen($encoded) > $this->max) { + $this->reduceData(); $this->buildJsonData(); $encoded = $this->encode($this->jsonData); $encoded = $this->assertEncodedLength($encoded); @@ -141,13 +151,12 @@ public function processLogEntry(LogEntry $logEntry) */ private function assertEncodedLength($encoded) { - $max = $this->getMaxLength(); - if (\strlen($encoded) <= $max) { + if (\strlen($encoded) <= $this->max) { return $encoded; } $this->jsonData['rows'] = [ [ - ['chromeLogger: unable to abridge log to ' . $this->debug->utility->getBytes($max)], + ['chromeLogger: unable to abridge log to ' . $this->debug->utility->getBytes($this->max)], null, 'warn', ], @@ -218,11 +227,9 @@ protected function getMaxLength() /** * Attempt to remove log entries to get header length < max * - * @param int $max maximum header length - * * @return void */ - protected function reduceData($max) + protected function reduceData() { \array_unshift($this->data['alerts'], new LogEntry( $this->debug, @@ -247,10 +254,10 @@ protected function reduceData($max) Data is now just alerts, summary, and errors */ $strlen = $this->calcHeaderSize(); - $avail = $max - $strlen; + $avail = $this->max - $strlen; if ($avail > 128) { // we've got enough room to fill with additional entries - $this->reduceDataFill($max, $logBack); + $this->reduceDataFill($logBack); } } @@ -293,45 +300,61 @@ private function reduceDataSummary() /** * Add back log entries until we're out of space * - * @param int $max maximum header length * @param array $logBack logEntries removed in initial pass * * @return void */ - protected function reduceDataFill($max, $logBack = array()) + protected function reduceDataFill($logBack = array()) { $indexes = \array_reverse(\array_keys($logBack)); - $depth = 0; - $groupOnly = false; + $this->depth = 0; + $this->groupOnly = false; /* work our way backwards through the log until we fill the avail header length */ foreach ($indexes as $i) { $logEntry = $logBack[$i]; - $method = $logEntry['method']; - if ($method === 'groupEnd') { - $depth++; - // https://bugs.xdebug.org/view.php?id=2095 - // phpcs:ignore SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions.NonFullyQualified - } elseif (in_array($method, ['group', 'groupCollapsed'], true)) { - $depth--; - } elseif ($groupOnly) { - continue; - } - $this->data['log'][$i] = $logEntry; - $strlen = $this->calcHeaderSize(); - if ($groupOnly && $depth === 0) { + $continue = $this->reduceDataFillWalk($logEntry, $i); + if ($continue === false) { break; } - if ($strlen + (40 * $depth) > $max) { - unset($this->data['log'][$i]); - $groupOnly = true; - } } \ksort($this->data['log']); $this->data['log'] = \array_values($this->data['log']); } + /** + * Add back log entries until we're out of space + * + * @param LogEntry $logEntry LogEntry instance + * @param int|string $index LogEntry index + * + * @return bool whether to continue + */ + private function reduceDataFillWalk(LogEntry $logEntry, $index) + { + $method = $logEntry['method']; + if ($method === 'groupEnd') { + $this->depth++; + // https://bugs.xdebug.org/view.php?id=2095 + // phpcs:ignore SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions.NonFullyQualified + } elseif (in_array($method, ['group', 'groupCollapsed'], true)) { + $this->depth--; + } elseif ($this->groupOnly) { + return true; + } + $this->data['log'][$index] = $logEntry; + $strlen = $this->calcHeaderSize(); + if ($this->groupOnly && $this->depth === 0) { + return false; + } + if ($strlen + (40 * $this->depth) > $this->max) { + unset($this->data['log'][$index]); + $this->groupOnly = true; + } + return true; + } + /** * Handle INF, Nan, & "undefined" * diff --git a/src/Debug/Route/Email.php b/src/Debug/Route/Email.php index 22f890ec..50be5409 100644 --- a/src/Debug/Route/Email.php +++ b/src/Debug/Route/Email.php @@ -63,7 +63,7 @@ public function getSubscriptions() */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $debug = $event->getSubject(); $this->debug->email( diff --git a/src/Debug/Route/Firephp.php b/src/Debug/Route/Firephp.php index fbaa7c82..340c5f1c 100644 --- a/src/Debug/Route/Firephp.php +++ b/src/Debug/Route/Firephp.php @@ -74,7 +74,7 @@ public function __construct(Debug $debug) */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $this->dumper->crateRaw = false; $this->outputEvent = $event; diff --git a/src/Debug/Route/Html.php b/src/Debug/Route/Html.php index 92e49ec9..88d217ac 100644 --- a/src/Debug/Route/Html.php +++ b/src/Debug/Route/Html.php @@ -141,7 +141,7 @@ public function getScript() */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); if ($event['isTarget'] === false) { return; diff --git a/src/Debug/Route/Script.php b/src/Debug/Route/Script.php index 0798e230..ed5af20f 100644 --- a/src/Debug/Route/Script.php +++ b/src/Debug/Route/Script.php @@ -70,7 +70,7 @@ public function __construct(Debug $debug) */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $this->dumper->crateRaw = false; $this->data = $this->debug->data->get(); diff --git a/src/Debug/Route/ServerLog.php b/src/Debug/Route/ServerLog.php index 07abad71..6aad1862 100644 --- a/src/Debug/Route/ServerLog.php +++ b/src/Debug/Route/ServerLog.php @@ -63,7 +63,7 @@ public function __construct(Debug $debug) */ public function processLogEntries($event = null) { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $this->dumper->crateRaw = false; $this->collectGarbage(); diff --git a/src/Debug/Route/Wamp.php b/src/Debug/Route/Wamp.php index 6b3bc677..44fd5805 100644 --- a/src/Debug/Route/Wamp.php +++ b/src/Debug/Route/Wamp.php @@ -249,7 +249,7 @@ public function onShutdown() */ public function processLogEntries($event = null) // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter { - $this->debug->php->assertType($event, 'bdk\PubSub\Event'); + $this->debug->utility->assertType($event, 'bdk\PubSub\Event'); $data = $this->debug->data->get(); foreach ($data['alerts'] as $logEntry) { diff --git a/src/Debug/Utility/Php.php b/src/Debug/Utility/Php.php index 47636ff6..b5655132 100644 --- a/src/Debug/Utility/Php.php +++ b/src/Debug/Utility/Php.php @@ -14,7 +14,6 @@ use bdk\Debug\Utility\Reflection; use Exception; -use InvalidArgumentException; use UnitEnum; /** @@ -31,33 +30,20 @@ class Php protected static $allowedClasses = []; /** - * Assert that a value is of a certain type + * Return the build date of the PHP binary * - * PHPDebugConsole supports an extreme range of PHP versions : 5.4 - 8.4 (and beyond) - * `func(MyObj $obj = null)` has been deprecated in PHP 8.4 - * must now be `func(?MyObj $obj = null)` (which is a php 7.1 feature) - * Workaround - remove type-hint when we allow null (not ideal) and call assertType - * When we drop support for php < 7.1, we can remove this method and do proper type-hinting - * - * @param mixed $value Value to test - * @param string $type "array", "callable", "object", or className - * @param bool $allowNull (true) allow null? - * - * @return void - * - * @throws InvalidArgumentException + * @return string|null */ - public static function assertType($value, $type, $allowNull = true) + public function buildDate() { - if (($allowNull && $value === null) || self::assertTypeCheck($value, $type)) { - return; - } - throw new InvalidArgumentException(\sprintf( - 'Expected %s%s, got %s', - $type, - $allowNull ? ' (or null)' : '', - self::getDebugType($value) - )); + \ob_start(); + \phpinfo(INFO_GENERAL); + $phpInfo = \ob_get_clean(); + $phpInfo = \strip_tags($phpInfo); + \preg_match('/Build Date (?:=> )?([^\n]*)/', $phpInfo, $matches); + return $matches + ? $matches[1] + : null; } /** @@ -240,28 +226,6 @@ public static function unserializeSafe($serialized, $allowedClasses = array()) return \unserialize($serialized); } - /** - * Test if value is of a certain type - * - * @param mixed $value Value to test - * @param string $type "array", "callable", "object", or className - * - * @return bool - */ - private static function assertTypeCheck($value, $type) - { - switch ($type) { - case 'array': - return \is_array($value); - case 'callable': - return \is_callable($value); - case 'object': - return \is_object($value); - default: - return \is_a($value, $type); - } - } - /** * Get friendly class name * diff --git a/src/Debug/Utility/PhpDoc.php b/src/Debug/Utility/PhpDoc.php index 603fba23..b11df59a 100644 --- a/src/Debug/Utility/PhpDoc.php +++ b/src/Debug/Utility/PhpDoc.php @@ -174,7 +174,7 @@ public static function hash($what) */ private function parse($comment, $reflector = null, $fullyQualifyType = 0, $sanitize = true) { - \bdk\Debug\Utility\Php::assertType($reflector, 'Reflector'); + \bdk\Debug\Utility::assertType($reflector, 'Reflector'); $this->reflector = $reflector; $this->fullyQualifyType = $fullyQualifyType; diff --git a/src/Debug/Utility/SerializeLog.php b/src/Debug/Utility/SerializeLog.php index 1d09d0cb..f4d56f47 100644 --- a/src/Debug/Utility/SerializeLog.php +++ b/src/Debug/Utility/SerializeLog.php @@ -44,7 +44,7 @@ class SerializeLog */ public static function import(array $data, $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); if (!$debug) { $debug = new Debug(); diff --git a/src/Debug/Utility/StringUtil.php b/src/Debug/Utility/StringUtil.php index 615b6a6d..fcb669cd 100644 --- a/src/Debug/Utility/StringUtil.php +++ b/src/Debug/Utility/StringUtil.php @@ -12,7 +12,7 @@ namespace bdk\Debug\Utility; -use bdk\Debug\Utility; +use bdk\HttpMessage\Utility\Stream as StreamUtility; use bdk\Debug\Utility\StringUtilHelperTrait; use bdk\HttpMessage\Utility\ContentType; use DOMDocument; @@ -94,7 +94,7 @@ public static function compare($valA, $valB, $operator = 'strnatcmp') public static function contentType($val) { if ($val instanceof StreamInterface) { - $val = Utility::getStreamContents($val); + $val = StreamUtility::getContents($val); } $finfo = new finfo(FILEINFO_MIME_TYPE); $contentType = $finfo->buffer($val); diff --git a/src/Debug/Utility/Table.php b/src/Debug/Utility/Table.php index 81ccbaef..22def356 100644 --- a/src/Debug/Utility/Table.php +++ b/src/Debug/Utility/Table.php @@ -87,7 +87,7 @@ class */ public function __construct($rows = array(), array $meta = array(), $debug = null) { - \bdk\Debug\Utility\Php::assertType($debug, 'bdk\Debug'); + \bdk\Debug\Utility::assertType($debug, 'bdk\Debug'); $this->debug = $debug ?: Debug::getInstance(); $this->initMeta($meta); diff --git a/src/Debug/Utility/Utility.php b/src/Debug/Utility/Utility.php index b99e17b9..b9166e74 100644 --- a/src/Debug/Utility/Utility.php +++ b/src/Debug/Utility/Utility.php @@ -13,8 +13,8 @@ namespace bdk\Debug; use bdk\Debug; -use Exception; -use Psr\Http\Message\StreamInterface; +use bdk\Debug\Utility\Php as PhpUtil; +use InvalidArgumentException; use RuntimeException; /** @@ -22,6 +22,36 @@ */ class Utility { + /** + * Assert that a value is of a certain type + * + * PHPDebugConsole supports an extreme range of PHP versions : 5.4 - 8.4 (and beyond) + * `func(MyObj $obj = null)` has been deprecated in PHP 8.4 + * must now be `func(?MyObj $obj = null)` (which is a php 7.1 feature) + * Workaround - remove type-hint when we allow null (not ideal) and call assertType + * When we drop support for php < 7.1, we can remove this method and do proper type-hinting + * + * @param mixed $value Value to test + * @param string $type "array", "callable", "object", or className + * @param bool $allowNull (true) allow null? + * + * @return void + * + * @throws InvalidArgumentException + */ + public static function assertType($value, $type, $allowNull = true) + { + if (($allowNull && $value === null) || self::assertTypeCheck($value, $type)) { + return; + } + throw new InvalidArgumentException(\sprintf( + 'Expected %s%s, got %s', + $type, + $allowNull ? ' (or null)' : '', + PhpUtil::getDebugType($value) + )); + } + /** * Emit headers queued for output directly using `header()` * @@ -143,6 +173,8 @@ public static function getEmittedHeader($key = 'Content-Type', $delimiter = ', ' /** * Returns sent/pending response headers * + * It is preferred to use PSR-7 (Http-Messaage) response interface over this method + * * The keys represent the header name as it will be sent over the wire, and * each value is an array of strings associated with the header. * @@ -160,27 +192,6 @@ public static function getEmittedHeaders() return $headers; } - /** - * Get stream contents without affecting pointer - * - * @param StreamInterface $stream StreamInterface - * - * @return string - */ - public static function getStreamContents(StreamInterface $stream) - { - try { - $pos = $stream->tell(); - $body = (string) $stream; // __toString() is like getContents(), but without throwing exceptions - $stream->seek($pos); - return $body; - // @codeCoverageIgnoreStart - } catch (Exception $e) { - return ''; - // @codeCoverageIgnoreEnd - } - } - /** * Get current git branch for specified directory * @@ -278,6 +289,28 @@ public static function sortFiles($files) return $files; } + /** + * Test if value is of a certain type + * + * @param mixed $value Value to test + * @param string $type "array", "callable", "object", or className + * + * @return bool + */ + private static function assertTypeCheck($value, $type) + { + switch ($type) { + case 'array': + return \is_array($value); + case 'callable': + return \is_callable($value); + case 'object': + return \is_object($value); + default: + return \is_a($value, $type); + } + } + /** * Emit a header * diff --git a/tests/Debug/Plugin/LogPhpTest.php b/tests/Debug/Plugin/LogPhpTest.php index be20b6d0..5616f923 100644 --- a/tests/Debug/Plugin/LogPhpTest.php +++ b/tests/Debug/Plugin/LogPhpTest.php @@ -47,22 +47,23 @@ public function testLogPhpInfo1() $logPhp->onBootstrap(new Event($this->debug)); $logEntries = $this->helper->deObjectifyData($this->debug->data->get('log')); - self::assertStringMatchesFormat( - \json_encode(['PHP Version', PHP_VERSION]), - \json_encode($logEntries[0]['args']) - ); - self::assertStringMatchesFormat( - \json_encode(['Server API', PHP_SAPI]), - \json_encode($logEntries[1]['args']) - ); - self::assertStringMatchesFormat( - \json_encode(['Build Date', '%s']), - \json_encode($logEntries[2]['args']) - ); - self::assertStringMatchesFormat( - \json_encode(['Thread Safe', PHP_ZTS ? 'yes' : 'no']), - \json_encode($logEntries[3]['args']) + + $tests = array( + ['PHP Version', PHP_VERSION], + ['Server API', PHP_SAPI], + ['Build Date', '%s'], // php >= 7.0 + ['Thread Safe', PHP_ZTS ? 'yes' : 'no'], ); + $buildDate = $this->debug->php->buildDate(); + if ($buildDate === null) { + \array_splice($tests, 2, 1); + } + foreach ($tests as $i => $expectArgs) { + self::assertStringMatchesFormat( + \json_encode($expectArgs), + \json_encode($logEntries[$i]['args']) + ); + } $found = array( 'dateTimezone' => null, diff --git a/tests/Debug/Utility/PhpTest.php b/tests/Debug/Utility/PhpTest.php index a99140e2..24cbe8d5 100644 --- a/tests/Debug/Utility/PhpTest.php +++ b/tests/Debug/Utility/PhpTest.php @@ -21,60 +21,6 @@ class PhpTest extends TestCase use AssertionTrait; use ExpectExceptionTrait; - /** - * @param mixed $value - * @param string $type - * @param bool $allowNull - * @param null|string $exceptionMessage - * - * @dataProvider providerAssertType - */ - public function testAssertType($value, $type, $allowNull = true, $exceptionMessage = null) - { - if ($exceptionMessage !== null) { - $this->expectException('InvalidArgumentException'); - $this->expectExceptionMessage($exceptionMessage); - } - Php::assertType($value, $type, $allowNull); - self::assertTrue(true); - } - - public function providerAssertType() - { - return [ - [array(), 'array', false], - ['call_user_func', 'callable', false], - [(object) array(), 'object', false], - [new \bdk\PubSub\Event(), 'bdk\PubSub\Event', false], - - [array(), 'array', true], - ['call_user_func', 'callable'], - [(object) array(), 'object', true], - [new \bdk\PubSub\Event(), 'bdk\PubSub\Event', true], - - [null, 'array', true ], - [null, 'callable', true], - [null, 'object', true], - [null, 'bdk\PubSub\Event', true], - - [null, 'array', false, 'Expected array, got null'], - [null, 'callable', false, 'Expected callable, got null'], - [null, 'object', false, 'Expected object, got null'], - [null, 'bdk\PubSub\Event', false, 'Expected bdk\PubSub\Event, got null'], - - [false, 'array', true, 'Expected array (or null), got bool'], - [false, 'callable', true, 'Expected callable (or null), got bool'], - [false, 'object', true, 'Expected object (or null), got bool'], - [false, 'bdk\PubSub\Event', true, 'Expected bdk\PubSub\Event (or null), got bool'], - - [false, 'array', false, 'Expected array, got bool'], - [false, 'callable', false, 'Expected callable, got bool'], - [false, 'object', false, 'Expected object, got bool'], - [false, 'bdk\PubSub\Event', false, 'Expected bdk\PubSub\Event, got bool'], - - ]; - } - /** * @param string $input * @param string $expect diff --git a/tests/Debug/Utility/UtilityTest.php b/tests/Debug/Utility/UtilityTest.php index c4c6fe93..9d898b8f 100644 --- a/tests/Debug/Utility/UtilityTest.php +++ b/tests/Debug/Utility/UtilityTest.php @@ -3,7 +3,6 @@ namespace bdk\Test\Debug\Utility; use bdk\Debug\Utility; -use bdk\HttpMessage\Stream; use bdk\PhpUnitPolyfill\ExpectExceptionTrait; use bdk\Test\Debug\DebugTestFramework; @@ -18,6 +17,60 @@ class UtilityTest extends DebugTestFramework { use ExpectExceptionTrait; + /** + * @param mixed $value + * @param string $type + * @param bool $allowNull + * @param null|string $exceptionMessage + * + * @dataProvider providerAssertType + */ + public function testAssertType($value, $type, $allowNull = true, $exceptionMessage = null) + { + if ($exceptionMessage !== null) { + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage($exceptionMessage); + } + Utility::assertType($value, $type, $allowNull); + self::assertTrue(true); + } + + public function providerAssertType() + { + return [ + [array(), 'array', false], + ['call_user_func', 'callable', false], + [(object) array(), 'object', false], + [new \bdk\PubSub\Event(), 'bdk\PubSub\Event', false], + + [array(), 'array', true], + ['call_user_func', 'callable'], + [(object) array(), 'object', true], + [new \bdk\PubSub\Event(), 'bdk\PubSub\Event', true], + + [null, 'array', true ], + [null, 'callable', true], + [null, 'object', true], + [null, 'bdk\PubSub\Event', true], + + [null, 'array', false, 'Expected array, got null'], + [null, 'callable', false, 'Expected callable, got null'], + [null, 'object', false, 'Expected object, got null'], + [null, 'bdk\PubSub\Event', false, 'Expected bdk\PubSub\Event, got null'], + + [false, 'array', true, 'Expected array (or null), got bool'], + [false, 'callable', true, 'Expected callable (or null), got bool'], + [false, 'object', true, 'Expected object (or null), got bool'], + [false, 'bdk\PubSub\Event', true, 'Expected bdk\PubSub\Event (or null), got bool'], + + [false, 'array', false, 'Expected array, got bool'], + [false, 'callable', false, 'Expected callable, got bool'], + [false, 'object', false, 'Expected object, got bool'], + [false, 'bdk\PubSub\Event', false, 'Expected bdk\PubSub\Event, got bool'], + + ]; + } + public function testEmitHeaders() { Utility::emitHeaders(array()); @@ -137,14 +190,6 @@ public function testGetEmittedHeaders() ), Utility::getEmittedHeaders()); } - public function testGetStreamContents() - { - $stream = new Stream('this is a test'); - $stream->seek(8); - self::assertSame('this is a test', Utility::getStreamContents($stream)); - self::assertSame('a test', $stream->getContents()); - } - public function testGitBranch() { $branch = Utility::gitBranch(); diff --git a/tests/ErrorHandler/EmailerTest.php b/tests/ErrorHandler/EmailerTest.php index 05df33ca..5b1031f1 100644 --- a/tests/ErrorHandler/EmailerTest.php +++ b/tests/ErrorHandler/EmailerTest.php @@ -124,7 +124,15 @@ public function testEmailOnError() \ksort($statsError); $statsFound = $this->errorHandler->stats->find($error); \ksort($statsFound); + + $tsLastOccurExpect = $statsError['tsLastOccur']; + $tsLastOccurActual = $statsFound['tsLastOccur']; + $tsDiff = \abs($tsLastOccurExpect - $tsLastOccurActual); + unset($statsError['tsLastOccur']); + unset($statsFound['tsLastOccur']); + self::assertSame($statsError, $statsFound); + self::assertLessThanOrEqual(1, $tsDiff); $this->errorHandler->setCfg('onError', null); }