Skip to content

Commit

Permalink
[DX] deprecated build shipped debug fuctions, allow using custom debu…
Browse files Browse the repository at this point in the history
…g package instead; add local d() and dd() functions (#4759)
  • Loading branch information
TomasVotruba committed Aug 10, 2023
1 parent 0bdace5 commit fa2a806
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"symfony/process": "^6.3",
"symplify/easy-parallel": "^11.1",
"symplify/rule-doc-generator-contracts": "^11.1",
"tracy/tracy": "^2.9",
"webmozart/assert": "^1.11"
},
"require-dev": {
Expand All @@ -68,13 +67,17 @@
"symplify/vendor-patches": "^11.2",
"tomasvotruba/class-leak": "^0.1",
"tomasvotruba/cognitive-complexity": "^0.1",
"tomasvotruba/lines": "0.3.1.72",
"tomasvotruba/type-coverage": "^0.2",
"tomasvotruba/unused-public": "^0.2"
"tomasvotruba/unused-public": "^0.2",
"tracy/tracy": "^2.9"
},
"replace": {
"rector/rector": "self.version",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-intl-grapheme": "*"
"symfony/polyfill-intl-grapheme": "*",
"symfony/error-handler": "*",
"symfony/var-dumper": "*"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -110,6 +113,7 @@
"rules-tests/Renaming/Rector/Name/RenameClassRector/Source"
],
"files": [
"tests/debug_functions.php",
"stubs/Directory.php",
"rules-tests/Transform/Rector/FuncCall/FuncCallToMethodCallRector/Source/some_view_function.php",
"rules-tests/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector/Source/FunctionTyped.php"
Expand Down
20 changes: 20 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,23 @@ parameters:

# iterable mix
- '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array<Rector\\Core\\Contract\\Rector\\RectorInterface>\) does not accept array\|iterable<Rector\\Core\\Contract\\Rector\\RectorInterface>#'

# false positive
- '#Parameter \#2 \$colors of method Tracy\\Dumper\\Renderer\:\:renderAsText\(\) expects array, array\|null given#'

# chicken/egg
-
message: '#Function "(d|dd)\(\)" cannot be used/left in the code#'
path: tests/debug_functions.php

# debug functions
-
message: '#Function "function_exists\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#'
path: tests/debug_functions.php

-
message: '#Do not name "d", shorter than 2 chars#'
path: tests/debug_functions.php

# not relevant
- '#Do not use static property#'
35 changes: 6 additions & 29 deletions src/functions/node_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,17 @@
use PhpParser\PrettyPrinter\Standard;
use Tracy\Dumper;

if (! function_exists('dump_with_depth')) {
function dump_with_depth(mixed $value, int $depth = 2): void
{
Dumper::dump($value, [
Dumper::DEPTH => $depth,
]);
}
}

if (! function_exists('dn')) {
function dn(Node $node, int $depth = 2): void
{
dump_node($node, $depth);
}
}


// @deprecated, use dump() or dd() instead
if (! function_exists('dump_node')) {
/**
* @param Node|Node[] $node
*/
function dump_node(Node | array $node, int $depth = 2): void
function dump_node(mixed $variable, int $depth = 2): never
{
$nodes = is_array($node) ? $node : [$node];

foreach ($nodes as $node) {
Dumper::dump($node, [
Dumper::DEPTH => $depth,
]);
}
trigger_error(
'This function is deprecated, to avoid enforcing of Rector debug package. Use your own favorite debugging package instead'
);
exit;
}
}


if (! function_exists('print_node')) {
/**
* @param Node|Node[] $node
Expand Down
24 changes: 24 additions & 0 deletions tests/debug_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Tracy\Dumper;

// helpful functions to create new rector rules

if (! function_exists('dd')) {
function dd(mixed $value, int $depth = 2): never
{
d($value, $depth);
die;
}
}

if (! function_exists('d')) {
function d(mixed $value, int $depth = 2): void
{
Dumper::dump($value, [
Dumper::DEPTH => $depth,
]);
}
}

0 comments on commit fa2a806

Please sign in to comment.