Skip to content

Instantly share code, notes, and snippets.

@cviebrock
cviebrock / find_class_strings.php
Last active September 13, 2024 00:21
PHP function that uses AST to try and find all instances of strings that are class names (so you can convert them to `MyClass::class`)
<?php declare(strict_types=1);
/**
* PHP script that uses composer files to find all classes in a project,
* then uses AST to try and find all instances of string expressions in the project,
* then filters that list of string expressions to those that match class names.
*
* The idea is to look for instances of:
*
* someFunction('MyClass');
@cviebrock
cviebrock / fun-with-pdo.md
Last active August 14, 2024 19:13
Fun with PDO
$sql = 'SELECT id, name, email FROM USERS';
$users = $db->query($sql)->fetchAll(PDO::FETCH_OBJ);
[
  0 => {id: 123, name: "John", email: "john@example.com"},
  1 => {id: 278, name: "Mary", email: "mary@example.com"},
  2 => {id: 390, name: "Evan", email: "evan@example.com"},
]
@cviebrock
cviebrock / crypter.php
Last active October 8, 2021 17:31
Replacement for Laravel 3.2's crypter class, that doesn't require mcrypt
<?php namespace Laravel; defined('DS') or die('No direct script access.');
/**
* The mcrypt library was deprecated in PHP7.2, so I couldn't run old Laravel 3.2
* applications anymore. Rather than upgrade the whole application, you can just
* replace the `/laravel/crypter.php` file with this one, which uses the
* OpenSSL library instead of mcrypt.
*/
@cviebrock
cviebrock / max-z-index.js
Created April 9, 2021 17:22
Get max z-index of all elements on a page
const reducer = (max, elem) => {
const s = window.getComputedStyle(elem);
const z = parseInt(s.getPropertyValue('z-index'), 10) || 0;
return Math.max(max, z);
};
const maxZ = Array.from(document.getElementsByTagName('*'))
.reduce(reducer, Number.MIN_SAFE_INTEGER);
@cviebrock
cviebrock / switch-php.sh
Created April 17, 2018 15:21
Helper to switch between brew versions of PHP
#!/usr/bin/env bash
CX="\033[0m"
CR="\033[31m"
CG="\033[32m"
CY="\033[33m"
FULL_VER=`php -r 'echo phpversion();'`
VER=`php -r 'echo phpversion();' | cut -d '.' -f1,2`
echo -e "${CY}Current PHP version:${CX} ${FULL_VER}";
@cviebrock
cviebrock / .powerlevelrc
Last active October 15, 2020 03:14
PowerLevel9K configuration
# General
POWERLEVEL9K_MODE='nerdfont-complete'
POWERLEVEL9K_COLOR_SCHEME='dark'
# Prompts
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir virtualenv vcs)
else
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(root_indicator dir virtualenv vcs)
fi
@cviebrock
cviebrock / test.js
Last active February 7, 2018 21:41
Test JS loop speed
var N = 100000,
array = Array.apply(null, {length: N}).map(Function.call, Math.random),
max = 0,
start = new Date().getTime(),
len = array.length;
for(var i=0; i<len; i++) {
max = Math.max(max, array[i]);
}
@cviebrock
cviebrock / DisableLazyLoading.php
Created August 16, 2017 18:39
Prevent lazy-loading Eloquent relations
<?php
/**
* Apply this trait to your models, and lazy-loading relations will throw an exception.
*/
trait DisableLazyLoading
{
protected function getRelationshipFromMethod($method)
{
@cviebrock
cviebrock / CompactFunction.php
Created August 16, 2017 14:14
Twig `compact` function
<?php
class CompactFunction extends Twig_Extension
{
/**
* @inheritdoc
*/
public function getFunctions()
{
@cviebrock
cviebrock / vincenty.sql
Created August 14, 2017 20:56
Vincenty distance formula as a MySQL function
CREATE FUNCTION VINCENTY(
lat1 FLOAT, lon1 FLOAT,
lat2 FLOAT, lon2 FLOAT
) RETURNS FLOAT
NO SQL
DETERMINISTIC
COMMENT 'Returns the distance in degrees on the Earth between two known points
of latitude and longitude using the Vincenty formula from:
http://en.wikipedia.org/wiki/Great-circle_distance'
BEGIN