Skip to content

Instantly share code, notes, and snippets.

<?php
namespace App\Helpers;
use Closure;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionFunction;
use ReflectionParameter;
use Throwable;
<?php
/**
* Recursively compare the content of an array, even if the elements, or nested elements, are out of order
*/
function assertArrayContent(array $expected, array $actual, $message = ''): void
{
($self = function (array $expected, array $actual, $parentIndex = null) use (&$self, $message): bool {
sort($expected);
sort($actual);
<?php
function compileQuery(Chess\DoctrineBundle\Doctrine\ORM\HintedQueryDecorator|\Doctrine\ORM\AbstractQuery $query) {
$sql = $query->getSQL();
$params = array_column(array_map(function (\Doctrine\ORM\Query\Parameter $parameter) {
/** @var mixed|\MyCLabs\Enum\Enum $value */
$value = $parameter->getValue();
$type = $parameter->getType();
$name = $parameter->getName();
<?php
namespace Tests\HighOrderReflection;
use BadMethodCallException;
use Closure;
use ReflectionClass;
use ReflectionException;
use Throwable;
use UnexpectedValueException;
@kmuenkel
kmuenkel / exceptionParser.php
Last active January 16, 2024 05:45
Parse a stack trace from debug_backtrace or an Exception in a more readable format
<?php
function stack(\Throwable|array $context = null, string $prune = 'vendor', int $text = 64, int $arr = 4): void {
$currentTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$current = array_shift($currentTrace);
$caller = implode(':', [$current['file'] ?? '', $current['line'] ?? '']);
$exception = $context instanceof \Throwable ? get_class($context) : '';
$message = $context instanceof \Throwable ? $context->getMessage() : '';
$trace = $context instanceof \Throwable ? $context->getTrace() : ($context ?: $currentTrace);
@kmuenkel
kmuenkel / entityToArray.php
Last active February 12, 2024 23:05
Entity to array
<?php
/**
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
$self = function entityToArray(
\Chess\WebBundle\Entity\EntityInterface|array $entity,
\Chess\WebBundle\Doctrine\ORM\EntityRepository|\Doctrine\ORM\EntityManager|string $manager = null,
\Psr\Container\ContainerInterface $container = null
@kmuenkel
kmuenkel / GenerateTableSupportCommand.php
Last active September 25, 2023 19:39
Table support generator aggragate
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class GenerateTableSupportCommand extends Command
{
<?php
namespace Database\Seeders;
use Str;
use File;
use SplFileInfo;
use ReflectionClass;
use ReflectionException;
use Illuminate\Database\Seeder;
@kmuenkel
kmuenkel / sortRecursiveHelper.php
Created May 24, 2023 03:36
Laravel Collection Macro to Sort Results and All Nested Entities
<?php
function sortRecursive()
{
$sortRecursive = function (callable $sortBy = null, $options = SORT_REGULAR, bool $descending = false): Collection {
/** @var Collection|Request $this */
$sort = $sortBy ? fn (array $item): Collection => collect($item)->sortBy($sortBy, $options, $descending) :
fn (array $item): Collection => $descending ? collect($item)->sortDesc() : collect($item)->sort();
@kmuenkel
kmuenkel / NodePath.php
Last active May 24, 2023 06:00
Dot-delimited recursive array data extraction with wildcard and nested expression support
<?php
namespace App\Helpers;
use stdClass;
class NodePath
{
protected static string $escaped = '\\\\';