From 1386fe5c4fb483ab796858fb0e273a6ecbd9e48f Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Mon, 31 Oct 2022 09:31:43 -0400 Subject: [PATCH] Php8 1 fixes (#497) * PHP 8.1 warning fixes Added #[\ReturnTypeWillChange] or appropriate return values Typed parameters where appropriate * Fixing unit tests to run without warnings * Update to more reasonable versions of PHP and PHPUnit (was missing PHPUnit 8) * mb_check_encoding param 1 can not be null * Fix SeekingData class for 8.1 * Update tests to not polute the temp directory * Revert PHP contrant * Tabs to spaces * Use Symfony\Component\VarDumper\Cloner\Data\SeekingData instead of local backported version Avoids Fatal error: Declaration of DebugBar\DataFormatter\VarDumper\SeekingData::seek($key) must be compatible with Symfony\Component\VarDumper\Cloner\Data::seek(string|int $key): ?static Co-authored-by: Barry vd. Heuvel --- composer.json | 4 +- .../DataCollector/AggregatedCollector.php | 17 ++- .../DataCollector/PDO/TraceablePDO.php | 45 ++++---- .../PDO/TraceablePDOStatement.php | 9 +- .../DataCollector/PDO/TracedStatement.php | 42 +++---- .../DataFormatter/DebugBarVarDumper.php | 4 +- .../DataFormatter/VarDumper/SeekingData.php | 103 ------------------ .../Tests/Storage/FileStorageTest.php | 35 +++++- 8 files changed, 85 insertions(+), 174 deletions(-) delete mode 100644 src/DebugBar/DataFormatter/VarDumper/SeekingData.php diff --git a/composer.json b/composer.json index 232e968..50793eb 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,10 @@ "require": { "php": "^7.1|^8", "psr/log": "^1|^2|^3", - "symfony/var-dumper": "^2.6|^3|^4|^5|^6" + "symfony/var-dumper": "^4|^5|^6" }, "require-dev": { - "phpunit/phpunit": "^7.5.20 || ^9.4.2", + "phpunit/phpunit": ">=7.5.20 <10.0", "twig/twig": "^1.38|^2.7|^3.0" }, "autoload": { diff --git a/src/DebugBar/DataCollector/AggregatedCollector.php b/src/DebugBar/DataCollector/AggregatedCollector.php index a289f43..56c9dc7 100644 --- a/src/DebugBar/DataCollector/AggregatedCollector.php +++ b/src/DebugBar/DataCollector/AggregatedCollector.php @@ -48,7 +48,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess /** * @param DataCollectorInterface $collector */ - public function addCollector(DataCollectorInterface $collector) + public function addCollector(DataCollectorInterface $collector) : void { $this->collectors[$collector->getName()] = $collector; } @@ -56,7 +56,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess /** * @return array */ - public function getCollectors() + public function getCollectors() : array { return $this->collectors; } @@ -66,7 +66,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess * * @param string $property */ - public function setMergeProperty($property) + public function setMergeProperty($property) : void { $this->mergeProperty = $property; } @@ -74,7 +74,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess /** * @return string */ - public function getMergeProperty() + public function getMergeProperty() : string { return $this->mergeProperty; } @@ -87,7 +87,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess * * @param bool|string $sort */ - public function setSort($sort) + public function setSort($sort) : void { $this->sort = $sort; } @@ -103,7 +103,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess /** * @return array */ - public function collect() + public function collect() : array { $aggregate = array(); foreach ($this->collectors as $collector) { @@ -123,7 +123,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess * @param array $data * @return array */ - protected function sort($data) + protected function sort($data) : array { if (is_string($this->sort)) { $p = $this->sort; @@ -142,7 +142,7 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess /** * @return string */ - public function getName() + public function getName() : string { return $this->name; } @@ -164,7 +164,6 @@ class AggregatedCollector implements DataCollectorInterface, ArrayAccess * @param mixed $key * @return mixed */ - #[\ReturnTypeWillChange] public function offsetGet($key) { diff --git a/src/DebugBar/DataCollector/PDO/TraceablePDO.php b/src/DebugBar/DataCollector/PDO/TraceablePDO.php index 044c6c0..753964a 100644 --- a/src/DebugBar/DataCollector/PDO/TraceablePDO.php +++ b/src/DebugBar/DataCollector/PDO/TraceablePDO.php @@ -23,14 +23,13 @@ class TraceablePDO extends PDO $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [TraceablePDOStatement::class, [$this]]); } - /** - * Initiates a transaction - * - * @link http://php.net/manual/en/pdo.begintransaction.php - * @return bool TRUE on success or FALSE on failure. - */ - #[\ReturnTypeWillChange] - public function beginTransaction() + /** + * Initiates a transaction + * + * @link http://php.net/manual/en/pdo.begintransaction.php + * @return bool TRUE on success or FALSE on failure. + */ + public function beginTransaction() : bool { return $this->pdo->beginTransaction(); } @@ -41,8 +40,7 @@ class TraceablePDO extends PDO * @link http://php.net/manual/en/pdo.commit.php * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function commit() + public function commit() : bool { return $this->pdo->commit(); } @@ -65,8 +63,7 @@ class TraceablePDO extends PDO * @link http://php.net/manual/en/pdo.errorinfo.php * @return array PDO::errorInfo returns an array of error information */ - #[\ReturnTypeWillChange] - public function errorInfo() + public function errorInfo() : array { return $this->pdo->errorInfo(); } @@ -107,8 +104,7 @@ class TraceablePDO extends PDO * @link http://php.net/manual/en/pdo.intransaction.php * @return bool TRUE if a transaction is currently active, and FALSE if not. */ - #[\ReturnTypeWillChange] - public function inTransaction() + public function inTransaction() : bool { return $this->pdo->inTransaction(); } @@ -182,8 +178,7 @@ class TraceablePDO extends PDO * @link http://php.net/manual/en/pdo.rollback.php * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function rollBack() + public function rollBack() : bool { return $this->pdo->rollBack(); } @@ -196,8 +191,7 @@ class TraceablePDO extends PDO * @param mixed $value * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function setAttribute($attribute, $value) + public function setAttribute($attribute, $value) : bool { return $this->pdo->setAttribute($attribute, $value); } @@ -210,6 +204,7 @@ class TraceablePDO extends PDO * @param array $args * @return mixed The result of the call */ + #[\ReturnTypeWillChange] protected function profileCall($method, $sql, array $args) { $trace = new TracedStatement($sql); @@ -241,7 +236,7 @@ class TraceablePDO extends PDO * * @param TracedStatement $stmt */ - public function addExecutedStatement(TracedStatement $stmt) + public function addExecutedStatement(TracedStatement $stmt) : void { $this->executedStatements[] = $stmt; } @@ -249,9 +244,9 @@ class TraceablePDO extends PDO /** * Returns the accumulated execution time of statements * - * @return int + * @return float */ - public function getAccumulatedStatementsDuration() + public function getAccumulatedStatementsDuration() : float { return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getDuration(); }); } @@ -261,7 +256,7 @@ class TraceablePDO extends PDO * * @return int */ - public function getMemoryUsage() + public function getMemoryUsage() : int { return array_reduce($this->executedStatements, function ($v, $s) { return $v + $s->getMemoryUsage(); }); } @@ -271,7 +266,7 @@ class TraceablePDO extends PDO * * @return int */ - public function getPeakMemoryUsage() + public function getPeakMemoryUsage() : int { return array_reduce($this->executedStatements, function ($v, $s) { $m = $s->getEndMemory(); return $m > $v ? $m : $v; }); } @@ -281,7 +276,7 @@ class TraceablePDO extends PDO * * @return TracedStatement[] */ - public function getExecutedStatements() + public function getExecutedStatements() : array { return $this->executedStatements; } @@ -291,7 +286,7 @@ class TraceablePDO extends PDO * * @return TracedStatement[] */ - public function getFailedExecutedStatements() + public function getFailedExecutedStatements() : array { return array_filter($this->executedStatements, function ($s) { return !$s->isSuccess(); }); } diff --git a/src/DebugBar/DataCollector/PDO/TraceablePDOStatement.php b/src/DebugBar/DataCollector/PDO/TraceablePDOStatement.php index 3250e58..011bbfe 100644 --- a/src/DebugBar/DataCollector/PDO/TraceablePDOStatement.php +++ b/src/DebugBar/DataCollector/PDO/TraceablePDOStatement.php @@ -62,8 +62,7 @@ class TraceablePDOStatement extends PDOStatement * @param mixed $driver_options [optional] * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null) + public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null) : bool { $this->boundParameters[$parameter] = $variable; $args = array_merge([$parameter, &$variable], array_slice(func_get_args(), 2)); @@ -82,8 +81,7 @@ class TraceablePDOStatement extends PDOStatement * constants. * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) + public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) : bool { $this->boundParameters[$parameter] = $value; return call_user_func_array(['parent', 'bindValue'], func_get_args()); @@ -99,8 +97,7 @@ class TraceablePDOStatement extends PDOStatement * @throws PDOException * @return bool TRUE on success or FALSE on failure. */ - #[\ReturnTypeWillChange] - public function execute($input_parameters = null) + public function execute($input_parameters = null) : bool { $preparedId = spl_object_hash($this); $boundParameters = $this->boundParameters; diff --git a/src/DebugBar/DataCollector/PDO/TracedStatement.php b/src/DebugBar/DataCollector/PDO/TracedStatement.php index 7f11fde..32fd870 100644 --- a/src/DebugBar/DataCollector/PDO/TracedStatement.php +++ b/src/DebugBar/DataCollector/PDO/TracedStatement.php @@ -32,7 +32,7 @@ class TracedStatement * @param array $params * @param string $preparedId */ - public function __construct($sql, array $params = [], $preparedId = null) + public function __construct(string $sql, array $params = [], $preparedId = null) { $this->sql = $sql; $this->parameters = $this->checkParameters($params); @@ -43,7 +43,7 @@ class TracedStatement * @param null $startTime * @param null $startMemory */ - public function start($startTime = null, $startMemory = null) + public function start($startTime = null, $startMemory = null) : void { $this->startTime = $startTime ?: microtime(true); $this->startMemory = $startMemory ?: memory_get_usage(false); @@ -55,7 +55,7 @@ class TracedStatement * @param float $endTime * @param int $endMemory */ - public function end(\Exception $exception = null, $rowCount = 0, $endTime = null, $endMemory = null) + public function end(\Exception $exception = null, int $rowCount = 0, float $endTime = null, int $endMemory = null) : void { $this->endTime = $endTime ?: microtime(true); $this->duration = $this->endTime - $this->startTime; @@ -71,10 +71,10 @@ class TracedStatement * @param array $params * @return array */ - public function checkParameters($params) + public function checkParameters(array $params) : array { foreach ($params as &$param) { - if (!mb_check_encoding($param, 'UTF-8')) { + if (!mb_check_encoding($param ?? '', 'UTF-8')) { $param = '[BINARY DATA]'; } } @@ -86,7 +86,7 @@ class TracedStatement * * @return string */ - public function getSql() + public function getSql() : string { return $this->sql; } @@ -97,7 +97,7 @@ class TracedStatement * @param string $quotationChar * @return string */ - public function getSqlWithParams($quotationChar = '<>') + public function getSqlWithParams(string $quotationChar = '<>') : string { if (($l = strlen($quotationChar)) > 1) { $quoteLeft = substr($quotationChar, 0, $l / 2); @@ -142,7 +142,7 @@ class TracedStatement * * @return int */ - public function getRowCount() + public function getRowCount() : int { return $this->rowCount; } @@ -152,7 +152,7 @@ class TracedStatement * * @return array */ - public function getParameters() + public function getParameters() : array { $params = []; foreach ($this->parameters as $name => $param) { @@ -166,7 +166,7 @@ class TracedStatement * * @return string */ - public function getPreparedId() + public function getPreparedId() : string { return $this->preparedId; } @@ -176,7 +176,7 @@ class TracedStatement * * @return boolean */ - public function isPrepared() + public function isPrepared() : bool { return $this->preparedId !== null; } @@ -184,7 +184,7 @@ class TracedStatement /** * @return float */ - public function getStartTime() + public function getStartTime() : float { return $this->startTime; } @@ -192,7 +192,7 @@ class TracedStatement /** * @return float */ - public function getEndTime() + public function getEndTime() : float { return $this->endTime; } @@ -202,7 +202,7 @@ class TracedStatement * * @return float */ - public function getDuration() + public function getDuration() : float { return $this->duration; } @@ -210,7 +210,7 @@ class TracedStatement /** * @return int */ - public function getStartMemory() + public function getStartMemory() : int { return $this->startMemory; } @@ -218,7 +218,7 @@ class TracedStatement /** * @return int */ - public function getEndMemory() + public function getEndMemory() : int { return $this->endMemory; } @@ -228,7 +228,7 @@ class TracedStatement * * @return int */ - public function getMemoryUsage() + public function getMemoryUsage() : int { return $this->memoryDelta; } @@ -238,7 +238,7 @@ class TracedStatement * * @return boolean */ - public function isSuccess() + public function isSuccess() : bool { return $this->exception === null; } @@ -248,8 +248,8 @@ class TracedStatement * * @return \Exception */ - public function getException() - { + public function getException() : \Exception + { return $this->exception; } @@ -268,7 +268,7 @@ class TracedStatement * * @return string */ - public function getErrorMessage() + public function getErrorMessage() : string { return $this->exception !== null ? $this->exception->getMessage() : ''; } diff --git a/src/DebugBar/DataFormatter/DebugBarVarDumper.php b/src/DebugBar/DataFormatter/DebugBarVarDumper.php index ee13263..7688799 100644 --- a/src/DebugBar/DataFormatter/DebugBarVarDumper.php +++ b/src/DebugBar/DataFormatter/DebugBarVarDumper.php @@ -4,7 +4,7 @@ namespace DebugBar\DataFormatter; use DebugBar\DataCollector\AssetProvider; use DebugBar\DataFormatter\VarDumper\DebugBarHtmlDumper; -use DebugBar\DataFormatter\VarDumper\SeekingData; +use Symfony\Component\VarDumper\Cloner\Data\SeekingData; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Cloner\VarCloner; @@ -254,8 +254,6 @@ class DebugBarVarDumper implements AssetProvider public function renderCapturedVar($capturedData, $seekPath = array()) { $data = unserialize($capturedData); - // The seek method was added in Symfony 3.2; emulate the behavior via SeekingData for older - // Symfony versions. if (!method_exists($data, 'seek')) { $data = new SeekingData($data->getRawData()); } diff --git a/src/DebugBar/DataFormatter/VarDumper/SeekingData.php b/src/DebugBar/DataFormatter/VarDumper/SeekingData.php deleted file mode 100644 index be71ebf..0000000 --- a/src/DebugBar/DataFormatter/VarDumper/SeekingData.php +++ /dev/null @@ -1,103 +0,0 @@ -getRawData(); - $item = $thisData[$this->position][$this->key]; - - if (!$item instanceof Stub || !$item->position) { - return; - } - $keys = array($key); - - switch ($item->type) { - case Stub::TYPE_OBJECT: - $keys[] = "\0+\0".$key; - $keys[] = "\0*\0".$key; - $keys[] = "\0~\0".$key; - $keys[] = "\0$item->class\0$key"; - case Stub::TYPE_ARRAY: - case Stub::TYPE_RESOURCE: - break; - default: - return; - } - - $data = null; - $children = $thisData[$item->position]; - - foreach ($keys as $key) { - if (isset($children[$key]) || array_key_exists($key, $children)) { - $data = clone $this; - $data->key = $key; - $data->position = $item->position; - break; - } - } - - return $data; - } - - /** - * {@inheritdoc} - */ - public function dump(DumperInterface $dumper) - { - // Override the base class dump to use the position and key - $refs = array(0); - $class = new \ReflectionClass($this); - $dumpItem = $class->getMethod('dumpItem'); - $dumpItem->setAccessible(true); - $data = $this->getRawData(); - $args = array($dumper, new Cursor(), &$refs, $data[$this->position][$this->key]); - $dumpItem->invokeArgs($this, $args); - } -} diff --git a/tests/DebugBar/Tests/Storage/FileStorageTest.php b/tests/DebugBar/Tests/Storage/FileStorageTest.php index 2061d14..4109cb0 100644 --- a/tests/DebugBar/Tests/Storage/FileStorageTest.php +++ b/tests/DebugBar/Tests/Storage/FileStorageTest.php @@ -7,14 +7,33 @@ use DebugBar\Storage\FileStorage; class FileStorageTest extends DebugBarTestCase { + private $dirname; + public function setUp(): void { - $this->dirname = '/tmp/debugbar'; - if (!file_exists($this->dirname)) { - mkdir($this->dirname, 0777); + $this->dirname = tempnam(sys_get_temp_dir(), 'debugbar'); + if (file_exists($this->dirname)) { + unlink($this->dirname); } + mkdir($this->dirname, 0777); $this->s = new FileStorage($this->dirname); $this->data = array('__meta' => array('id' => 'foo')); + $this->s->save('bar', $this->data); + } + + public function teardown(): void + { + $files = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($this->dirname, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $fileinfo) { + $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); + $todo($fileinfo->getRealPath()); + } + + rmdir($this->dirname); } public function testSave() @@ -26,7 +45,7 @@ class FileStorageTest extends DebugBarTestCase public function testGet() { - $data = $this->s->get('foo'); + $data = $this->s->get('bar'); $this->assertEquals($this->data, $data); } @@ -39,6 +58,12 @@ class FileStorageTest extends DebugBarTestCase public function testClear() { $this->s->clear(); - $this->assertFileNotExists($this->dirname . '/foo.json'); + + // avoid depreciation message on newer PHPUnit versions. Can be removed after + if (method_exists($this, 'assertFileDoesNotExist')) { + $this->assertFileDoesNotExist($this->dirname . '/foo.json'); + } else { + $this->assertFileNotExists($this->dirname . '/foo.json'); + } } }