Skip to content

Instantly share code, notes, and snippets.

View ghostwriter's full-sized avatar
🐘
while(!succeed=try())

Nathanael Esayeas ghostwriter

🐘
while(!succeed=try())
View GitHub Profile
@ghostwriter
ghostwriter / composer-outdated.md
Created February 25, 2023 23:47
Retrieve a list outdated composer packages in json

Retrieve a list outdated composer packages as JSON

composer outdated --direct -f json

output:

No outout if the latest packages are installed.

@ghostwriter
ghostwriter / create-tests.sh
Created February 14, 2023 05:01
Generate test files for each php file in `src` directory.
#!/usr/bin/env bash
set -e
find src -name '*.php' -not \( -path '*Trait.php' -prune \) -not \( -path '*/Abstract*' -prune \) -not \( -path '*/Traits/*' -prune \) -not \( -path '*/Contract/*' -prune \) -type f -print0 | while IFS= read -r -d $'\0' file;
do
file="tests/Unit/${file:(4)}";
path="${file/.php/Test.php}";
if [[ -e $path ]]; then
@ghostwriter
ghostwriter / MezzioTestcase.php
Created January 30, 2023 21:43
PHPUnit Testcase for Mezzio Applications
<?php
declare(strict_types=1);
namespace Ghostwriter\Mezzio\Workbench;
use Mezzio\Application;
use Mezzio\MiddlewareFactory;
use Mezzio\Router\RouteResult;
use Mezzio\Router\RouterInterface;
@ghostwriter
ghostwriter / in_range.php
Created November 13, 2022 23:47
InRange
/**
* Check if value is between given range.
*
* @param mixed $value
* @param mixed $min
* @param mixed $max
*
* @return bool
*/
function in_range($value, $min, $max): bool {
@ghostwriter
ghostwriter / poc.php
Created September 25, 2022 01:47
Node finder with hash
<?php
declare(strict_types=1);
$nodeAddress = spl_object_hash($node);
$nodeFinder = new PhpParser\NodeFinder;
$foundNode = $nodeFinder->findFirst($nodes, function (Node $node) use ($nodeAddress) {
return spl_object_hash($node) === $nodeAddress;
@ghostwriter
ghostwriter / backup-mysql-docker-container.sh
Created July 15, 2022 01:06
Backup and restore a MySQL database in a Docker Container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
<?php
// AssertableValueObject.php
final class AssertionsFailedException extends \RuntimeException
{
}
interface AssertableInterface
{
<?php
declare(strict_types=1);
namespace Ghostwriter\Collection;
use Closure;
use Ghostwriter\Collection\Contract\CollectionInterface;
use InvalidArgumentException;
use Traversable;
<?php
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$visitor = new PhpParser\NodeVisitor\FindingVisitor(function (Node $node) {
return $node instanceof Node\Expr\MethodCall && $node->name instanceof Identifier;
});