Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Collington amnuts

View GitHub Profile
@amnuts
amnuts / DateTimeInterval.php
Created Dec 14, 2021
Extend the \DateInterval class to have a __toString() method, returning the string representation of the object
View DateTimeInterval.php
<?php
use DateInterval;
class DateTimeInterval extends DateInterval
{
/**
* Return the textual representation of an interval object
*
* @return string
View isM1.py
import platform
import subprocess
def isMac() -> bool:
return platform.system() == 'Darwin'
def getMacProcessor() -> str:
return subprocess.check_output(['sysctl', '-n', 'machdep.cpu.brand_string']).decode('utf-8').rstrip()
View 01_setting_up_wsl.md

These are just some notes on my setting up of WSL

  • 02_new_location.md
    I wanted to run WSL and the WSL docker containers from a location that wasn't my C drive, and this explains how
  • 03_defender.md
    Excluding WSL files from Windows Defender apparently helps to increase speed
  • 04_setup.md
    General commands to set things up that I wanted
@amnuts
amnuts / UnitTestCase.php
Created Oct 14, 2021
PHPUnit: mock an iterator
View UnitTestCase.php
<?php
namespace Tests;
use ArrayIterator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class UnitTestCase extends TestCase
{
@amnuts
amnuts / warm.php
Last active Apr 27, 2021
Fake pre-warm op-cache files for testing https://github.com/amnuts/opcache-gui
View warm.php
<?php
$nums = range(0, 5000);
foreach ($nums as $i) {
$path = __DIR__."/files/c{$i}.php";
if (file_exists($path)) {
break;
}
file_put_contents($path, "<?php\n\nfunction c{$i}() { return {$i}; }\n\n");
@amnuts
amnuts / propSearch.js
Created Oct 6, 2020
Helpers to see if object has all props or any of the props
View propSearch.js
const hasAllProps = function(list, within) {
list.forEach(function(e) {
if (!within.hasOwnProperty(e) || within[e] == "") {
return false;
}
});
return true;
};
const hasAnyProps = function(list, within) {
@amnuts
amnuts / directory-reflection.php
Created Jan 6, 2020
Using Better Reflection to get details on classes in multiple directories
View directory-reflection.php
<?php
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
require __DIR__ . '/vendor/autoload.php';
$refSubscriptions = [];
@amnuts
amnuts / datetime.php
Created Jun 4, 2019
PHP date/time conversion example
View datetime.php
<?php
foreach (range(1, 12) as $m) {
$elStart = new \DateTimeImmutable("2019-{$m}-10 23:50:00", new \DateTimeZone('Europe/London'));
$elEnd = $elStart->add(new \DateInterval('PT5H'));
$utcStart = $elStart->setTimezone(new \DateTimeZone('UTC'));
$utcEnd = $elEnd->setTimezone(new \DateTimeZone('UTC'));
printf("E/L: %s / %s, UTC: %s / %s\n",
$elStart->format(DATE_RFC822),
$elEnd->format(DATE_RFC822),
@amnuts
amnuts / stuff.php
Last active Mar 10, 2021
jotting down functions
View stuff.php
<?php
// Return greatest common divider of two numbers
function gcd($a, $b) {
return $b ? gcd($b, $a % $b) : $a;
}
// Returns the least common multiple of two or more numbers.
function lcm(...$numbers): int
{
@amnuts
amnuts / timer.php
Last active Oct 26, 2018
Simple PHP timer class
View timer.php
<?php
/**
* Utility class to enable very simplistic timing.
*
* Usage example:
*
* $t = new Timer();
* // do something here
* echo $t;