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

Copy Debug class from doctrine/common #11000

Merged
merged 1 commit into from
Oct 14, 2023
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
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Doctrine\ORM\Tools\Console\Command;

use Doctrine\Common\Util\Debug;
use Doctrine\ORM\Tools\Console\CommandCompatibility;
use Doctrine\ORM\Tools\Debug;
use LogicException;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -116,7 +116,7 @@ private function doExecute(InputInterface $input, OutputInterface $output): int

$resultSet = $query->execute([], constant($hydrationMode));

$ui->text(Debug::dump($resultSet, (int) $input->getOption('depth'), true, false));
$ui->text(Debug::dump($resultSet, (int) $input->getOption('depth')));

return 0;
}
Expand Down
168 changes: 168 additions & 0 deletions lib/Doctrine/ORM/Tools/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Tools;

use ArrayIterator;
use ArrayObject;
use DateTimeInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
use Doctrine\Persistence\Proxy;
use stdClass;

use function array_keys;
use function count;
use function end;
use function explode;
use function extension_loaded;
use function get_class;
use function html_entity_decode;
use function ini_get;
use function ini_set;
use function is_array;
use function is_object;
use function ob_end_clean;
use function ob_get_contents;
use function ob_start;
use function strip_tags;
use function var_dump;

/**
* Static class containing most used debug methods.
*
* @internal
*
* @link www.doctrine-project.org
*/
final class Debug
{
/**
* Private constructor (prevents instantiation).
*/
private function __construct()

Check warning on line 44 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L44

Added line #L44 was not covered by tests
{
}

Check warning on line 46 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L46

Added line #L46 was not covered by tests

/**
* Prints a dump of the public, protected and private properties of $var.
*
* @link https://xdebug.org/
*
* @param mixed $var The variable to dump.
* @param int $maxDepth The maximum nesting level for object properties.
*/
public static function dump($var, int $maxDepth = 2): string

Check warning on line 56 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L56

Added line #L56 was not covered by tests
{
$html = ini_get('html_errors');

Check warning on line 58 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L58

Added line #L58 was not covered by tests

if ($html !== '1') {
ini_set('html_errors', 'on');

Check warning on line 61 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L60-L61

Added lines #L60 - L61 were not covered by tests
}

if (extension_loaded('xdebug')) {
$previousDepth = ini_get('xdebug.var_display_max_depth');
ini_set('xdebug.var_display_max_depth', (string) $maxDepth);

Check warning on line 66 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L64-L66

Added lines #L64 - L66 were not covered by tests
derrabus marked this conversation as resolved.
Show resolved Hide resolved
}

try {
$var = self::export($var, $maxDepth);

Check warning on line 70 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L70

Added line #L70 was not covered by tests

ob_start();
var_dump($var);

Check warning on line 73 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L72-L73

Added lines #L72 - L73 were not covered by tests

$dump = ob_get_contents();

Check warning on line 75 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L75

Added line #L75 was not covered by tests

ob_end_clean();

Check warning on line 77 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L77

Added line #L77 was not covered by tests

$dumpText = strip_tags(html_entity_decode($dump));
} finally {
ini_set('html_errors', $html);

Check warning on line 81 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L79-L81

Added lines #L79 - L81 were not covered by tests

if (isset($previousDepth)) {
ini_set('xdebug.var_display_max_depth', $previousDepth);

Check warning on line 84 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L83-L84

Added lines #L83 - L84 were not covered by tests
}
}

return $dumpText;

Check warning on line 88 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L88

Added line #L88 was not covered by tests
}

/**
* @param mixed $var
*
* @return mixed
*/
public static function export($var, int $maxDepth)
{
if ($var instanceof Collection) {
$var = $var->toArray();
derrabus marked this conversation as resolved.
Show resolved Hide resolved
}

if (! $maxDepth) {
return is_object($var) ? get_class($var)
: (is_array($var) ? 'Array(' . count($var) . ')' : $var);
}

if (is_array($var)) {
$return = [];

foreach ($var as $k => $v) {
$return[$k] = self::export($v, $maxDepth - 1);
}

return $return;
}

if (! is_object($var)) {
return $var;
}

$return = new stdClass();
if ($var instanceof DateTimeInterface) {
$return->__CLASS__ = get_class($var);
$return->date = $var->format('c');
$return->timezone = $var->getTimezone()->getName();

return $return;
}

$return->__CLASS__ = DefaultProxyClassNameResolver::getClass($var);

if ($var instanceof Proxy) {
$return->__IS_PROXY__ = true;
$return->__PROXY_INITIALIZED__ = $var->__isInitialized();

Check warning on line 134 in lib/Doctrine/ORM/Tools/Debug.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Tools/Debug.php#L133-L134

Added lines #L133 - L134 were not covered by tests
}

if ($var instanceof ArrayObject || $var instanceof ArrayIterator) {
$return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
}

return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
}

/**
* Fill the $return variable with class attributes
* Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075}
*
* @param object $var
*
* @return mixed
*/
private static function fillReturnWithClassAttributes($var, stdClass $return, int $maxDepth)
{
$clone = (array) $var;

foreach (array_keys($clone) as $key) {
$aux = explode("\0", (string) $key);
$name = end($aux);
if ($aux[0] === '') {
$name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private');
}

$return->$name = self::export($clone[$key], $maxDepth - 1);
}

return $return;
}
}
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps">
<exclude-pattern>lib/Doctrine/ORM/Tools/Debug.php</exclude-pattern>
<exclude-pattern>tests/Doctrine/Tests/ORM/Tools/DebugTest.php</exclude-pattern>
</rule>

<rule ref="Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase">
<exclude-pattern>lib/Doctrine/ORM/Events.php</exclude-pattern>
<exclude-pattern>lib/Doctrine/ORM/Tools/ToolEvents.php</exclude-pattern>
Expand Down
7 changes: 1 addition & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@
<ArgumentTypeCoercion>
<code>$class</code>
<code>$class</code>
<code><![CDATA[new $definition['class']()]]></code>
<code>$platformFamily</code>
<code><![CDATA[new $definition['class']()]]></code>
</ArgumentTypeCoercion>
<DeprecatedClass>
<code>new UuidGenerator()</code>
Expand Down Expand Up @@ -2415,11 +2415,6 @@
<code>getAllClassNames</code>
</PossiblyNullReference>
</file>
<file src="lib/Doctrine/ORM/Tools/Console/Command/RunDqlCommand.php">
<DeprecatedClass>
<code><![CDATA[Debug::dump($resultSet, (int) $input->getOption('depth'), true, false)]]></code>
</DeprecatedClass>
</file>
<file src="lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php">
<InvalidNullableReturnType>
<code>int</code>
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<file name="lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php"/>
</errorLevel>
</DuplicateClass>
<ForbiddenCode>
<errorLevel type="suppress">
<file name="lib/Doctrine/ORM/Tools/Debug.php"/>
</errorLevel>
</ForbiddenCode>
<InvalidArgument>
<errorLevel type="suppress">
<!-- Argument type changes in DBAL 3.2 -->
Expand Down
Loading