Skip to content

Instantly share code, notes, and snippets.

View itsjavi's full-sized avatar

Javi Aguilar itsjavi

View GitHub Profile
@itsjavi
itsjavi / enable_xdebug.sh
Created September 29, 2021 18:37
Enable xdebug in a PHP Docker container
#!/usr/bin/env bash
pecl install xdebug
docker-php-ext-enable xdebug
echo -e "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so\n\
xdebug.mode=coverage\n\
xdebug.remote_enable=1\n\
xdebug.remote_autostart=1\n\
xdebug.remote_connect_back=0\n\
xdebug.remote_port=9001\n\
@itsjavi
itsjavi / Component.jsx
Last active June 15, 2022 01:18
WebStorm template for React Components and React CSS Modules. You'll have to enable Live Templates for the END cursor to work.
import React from 'react';
import PropTypes from "prop-types";
import styles from './${NAME}.module.css';
#set($cssClass = ${StringUtils.removeAndHump(${NAME}, ".")})
#set($cssClass = $cssClass.substring(0,1).toLowerCase() + $cssClass.substring(1))
function ${NAME}(props) {
return (
<div className={styles.${cssClass}}>
#[[$END$]]#
@itsjavi
itsjavi / GridPositionCalculator.php
Last active January 29, 2021 01:11
PHP Grid cell position calculator - To help distributing a flat list of elements into 2D tables (grids)
<?php
declare(strict_types=1);
namespace App\Support;
class GridPositionCalculator
{
/**
* Given the sequential index of the element in a flattened list,
@itsjavi
itsjavi / PercentileRankCalculator.php
Last active January 29, 2021 01:12
PHP Percentile Rank calculator class for collections of associative arrays
<?php
declare(strict_types=1);
namespace App\Support;
class PercentileRankCalculator
{
/**
* Calculate percentile rank over a field in a collection of associative arrays
@itsjavi
itsjavi / JsonEncoder.php
Last active March 5, 2021 15:43
PHP json_encode prettify compact mode with indentation level options
<?php
declare(strict_types=1);
namespace App\Support;
class JsonEncoder
{
private const JSON_PRETTY_PRINT_INDENT = 4;
@itsjavi
itsjavi / pandoc.css
Created October 31, 2020 08:19 — forked from killercup/pandoc.css
Add this to your Pandoc HTML documents using `--css pandoc.css` to make them look more awesome. (Tested with Markdown and LaTeX.)
/*
* I add this to html files generated with pandoc.
*/
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
@itsjavi
itsjavi / detect-changed-lines.sh
Created May 26, 2020 14:51
detect changed lines (additions + deletions) in git
#!/usr/bin/env bash
changed_lines_threshold=4
changed_lines=$(git diff --numstat --pretty="%H" HEAD^ HEAD | awk 'NF==3 {numlines+=$1+$2} END {printf("%d", numlines)}')
if [[ $changed_lines -gt $changed_lines_threshold ]]]; then
echo "ERROR: Source code changes detected after last command (${changed_lines} lines). Aborting.";
exit 1;
fi
@itsjavi
itsjavi / quick_debug.php
Last active January 2, 2020 10:55
quick debug php function
<?php
function quick_debug(string $message = '', array $params = [])
{
$file = __DIR__ . '/debug-dev.log';
$date = date('Y-m-d H:i:s.u');
$params = array_map(function ($data) {
if (is_scalar($data)) {
return $data;
@itsjavi
itsjavi / AbstractDto.php
Created April 3, 2018 12:51
basic DTO concept in PHP
<?php
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
class AbstractDto extends ArrayableJsonableObject implements \ArrayAccess
{
public function __construct(array $properties = [])
{
@itsjavi
itsjavi / service_proxy_pattern.php
Last active May 29, 2017 01:02
PHP Service Proxy + Lazy Proxy patterns
<?php
namespace Foo;
use BadMethodCallException;
use Closure;
use DomainException;
/*
* This example encapsulates before/after method call callbacks inside a the service itself, using a proxy,