1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-16 21:08:34 +01:00

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 <barryvdh@gmail.com>
This commit is contained in:
Bruce Wells 2022-10-31 09:31:43 -04:00 committed by GitHub
parent fd43dfb084
commit 1386fe5c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 174 deletions

View File

@ -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": {

View File

@ -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)
{

View File

@ -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(); });
}

View File

@ -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;

View File

@ -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() : '';
}

View File

@ -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());
}

View File

@ -1,103 +0,0 @@
<?php
namespace DebugBar\DataFormatter\VarDumper;
use Symfony\Component\VarDumper\Cloner\Cursor;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\DumperInterface;
use Symfony\Component\VarDumper\Cloner\Stub;
/**
* This class backports the seek() function from Symfony 3.2 to older versions - up to v2.6. The
* class should not be used with newer Symfony versions that provide the seek function, as it relies
* on a lot of undocumented functionality.
*/
class SeekingData extends Data
{
// Because the class copies/pastes the seek() implementation from Symfony 3.2, we reproduce its
// copyright here; this class is subject to the following additional copyright:
/*
* Copyright (c) 2014-2017 Fabien Potencier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
private $position = 0;
private $key = 0;
/**
* Seeks to a specific key in nested data structures.
*
* @param string|int $key The key to seek to
*
* @return self|null A clone of $this of null if the key is not set
*/
public function seek($key)
{
$thisData = $this->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);
}
}

View File

@ -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');
}
}
}