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 / phpstorm.bat
Last active Jun 22, 2021
Add context menu to Windows 7 to open file/folder in PhpStorm
View phpstorm.bat
@echo off
SET PhpStormPath=C:\Program Files (x86)\JetBrains\PhpStorm 8.0.2\bin\PhpStorm64.exe
echo Adding file entries
@reg add "HKEY_CLASSES_ROOT\*\shell\Open in PhpStorm" /t REG_SZ /v "" /d "Open in PhpStorm" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open in PhpStorm" /t REG_EXPAND_SZ /v "Icon" /d "%PhpStormPath%,0" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open in PhpStorm\command" /t REG_SZ /v "" /d "%PhpStormPath% \"%%1\"" /f
echo Adding folder entries
@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 / scraping.php
Last active Apr 21, 2021
Example of how to scrape multiple pages using Zend\Dom from Zend Framework 2.
View scraping.php
<?php
use \Zend\Dom\Query;
use \Zend\Debug\Debug;
/**
* Fetch the page source and cache it, ensuring it's saved as UTF-8
*
* @param string $url
* @return string
@amnuts
amnuts / merge_urls.php
Last active Mar 11, 2021
Merge two urls using PHP
View merge_urls.php
<?php
/**
* Combine two urls.
*
* The urls can be either a string or url parts that consist of:
*
* scheme, host, port, user, pass, path, query, fragment
*
* If passed in as parts in an array, the query parameter can be either
@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 / ofilter.php
Last active Jan 11, 2021
Filter an array of objects based on property, or multiple property values
View ofilter.php
<?php
/**
* Filter an array of objects.
*
* You can pass in one or more properties on which to filter.
*
* If the key of an array is an array, then it will filtered down to that
* level of node.
*