Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjamin Cremer bcremer

View GitHub Profile
@bcremer
bcremer / phpstorm_2020.3-PKGBUILD.patch
Created Dec 3, 2020
phpstorm_2020.3-PKGBUILD.patch
View phpstorm_2020.3-PKGBUILD.patch
diff --git PKGBUILD PKGBUILD
index 59e965d..feb6731 100644
--- PKGBUILD
+++ PKGBUILD
@@ -6,7 +6,7 @@
pkgbase=phpstorm
pkgname=(phpstorm phpstorm-jre)
-pkgver=2020.2.4b202.8194.11
+pkgver=2020.3b203.5981.175
@bcremer
bcremer / Sieve_of_Eratosthenes.php
Created Aug 27, 2020
Generate prime numbers using the Sieve of Eratosthenes, Lazily evaluted with PHP
View Sieve_of_Eratosthenes.php
<?php
// Generate prime numbers using the Sieve of Eratosthenes
// Lazily evaluted
// Inspired by: https://www.youtube.com/watch?v=5jwV3zxXc8E
$primes = sieve(nat(2));
foreach ($primes as $value) {
echo $value . PHP_EOL;
@bcremer
bcremer / 0_output.log
Last active Apr 20, 2020
Measure array unpack vs. array_merge
View 0_output.log
Array Unpack
user: 4.355 ms
system: 0 ms
wait: 25 ms
total: 4.381 ms
Array Merge
user: 6.798 ms
system: 0 ms
wait: 7 ms
@bcremer
bcremer / BaseTestCase.php
Created Apr 7, 2020
Reset PHPUnit testcase properties to save memory after each test.
View BaseTestCase.php
<?php
abstract class BaseTestCase extends PHPUnit\Framework\TestCase
{
/**
* Reset the properties to save memory after each test.
* @see https://kriswallsmith.net/post/18029585104/faster-phpunit
*/
protected function tearDown(): void
{
View php-pslam-functional-generators.php
<?php
declare(strict_types=1);
/**
* @psalm-template TKey
* @psalm-template TValue
* @psalm-param iterable<TKey, TValue> $list
* @psalm-param callable(TValue): bool $filter
* @psalm-return Generator<TKey, TValue>
*/
View ContainerTest.php
<?php
declare(strict_types=1);
namespace AppTest;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use function assert;
use function strpos;
@bcremer
bcremer / measure_cpu_time.php
Last active May 8, 2020
Measure CPU Time / User / System Time in PHP Scripts
View measure_cpu_time.php
<?php
$measure = function (callable $lambda) {
$rUsage = getrusage();
$startUTime = $rUsage['ru_utime.tv_usec'] + $rUsage['ru_utime.tv_sec'] * 1000000;
$startSTime = $rUsage['ru_stime.tv_usec'] + $rUsage['ru_stime.tv_sec'] * 1000000;
$startHrTime = hrtime(true);
$lambda();
@bcremer
bcremer / mysql-alternative-insert-syntax.sql
Last active Feb 4, 2019
MySQL alternative insert syntax
View mysql-alternative-insert-syntax.sql
-- Traditional insert
INSERT INTO `campaigns` (name, active) VALUES ('foobar', 1⁾;
-- Alternative insert
INSERT INTO `campaigns` SET name = 'foobar', active = 1;
@bcremer
bcremer / IEEE754.php
Last active Jan 29, 2019
Why IEEE 754 floating point numbers are a mess to debug
View IEEE754.php
<?php
// Live Demo: https://3v4l.org/GWJFZ
// see also: http://0.30000000000000004.com/
var_dump(0.1 + 0.2); // float(0.3)
var_dump(0.3); // float(0.3)
var_dump(0.1 + 0.2 == 0.3); // bool(false)
// Set the number of significant digits displayed in floating point numbers.
ini_set('precision', 18);
View IsNotInThePastValidation.php
<?php
// $clock implements Clock
// interface Clock
// {
// public function now() : DateTimeImmutable;
// }
// setup
$startDate = Date::fromString('2019-01-04');