Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjamin Morel BenMorel

View GitHub Profile
@BenMorel
BenMorel / SafeCast.php
Created Feb 26, 2021
Safe casting to int (WIP)
View SafeCast.php
<?php
final class SafeCast
{
/**
* Converts the given variable to an integer.
*
* Allowed types are int, float and string.
*
* - int values are returned as is;
View composer.json
{
"require": {
"doctrine/orm": "^2.8",
"brick/geo": "^0.4.0"
}
}
@BenMorel
BenMorel / generate-Psalm-CallMap.php
Last active Dec 28, 2020
Generates entries for Psalm's CallMap from Reflection
View generate-Psalm-CallMap.php
<?php
// Configure here the classes to export. Example:
$classes = [
'GEOSGeometry',
'GEOSWKBReader',
'GEOSWKBWriter',
'GEOSWKTReader',
'GEOSWKTWriter'
];
@BenMorel
BenMorel / tokenize-php-script.php
Last active Dec 9, 2020
Tokenizes a PHP script with token_get_all(), and outputs a readable representation.
View tokenize-php-script.php
<?php
/**
* Tokenizes a PHP script with token_get_all(), and outputs a readable representation.
*
* Usage: tokenize-php-script.php FILE.php
*
* Sample output:
*
* 1 T_OPEN_TAG <?php\n
@BenMorel
BenMorel / getDominantColor.php
Last active Dec 3, 2020
Get dominant color from an Imagick image
View getDominantColor.php
<?php
/**
* Two methods to get the dominant color from an image.
* Useful to create placeholders while loading an image.
*
* Method 1 yields more bright colors than method 2.
*/
/**
@BenMorel
BenMorel / errorLevelToConstants.php
Created Dec 2, 2020
Returns constants matching an error level
View errorLevelToConstants.php
<?php
/**
* Examples:
*
* errorLevelToConstants(E_WARNING) => ['E_WARNING', 'E_ALL']
* errorLevelToConstants(3) => ['E_ERROR', 'E_WARNING', 'E_ALL']
*
* @return string[]
*/
@BenMorel
BenMorel / ca.crt
Created Nov 27, 2020
My public dev certificate
View ca.crt
-----BEGIN CERTIFICATE-----
MIIDdTCCAl2gAwIBAgIUN09wV0sbUhrzQ9WonV0xOabJqJ8wDQYJKoZIhvcNAQEL
BQAwSjELMAkGA1UEBhMCVVMxGDAWBgNVBAoMD19EZXZlbG9wbWVudCBDQTEhMB8G
A1UEAwwYRGV2ZWxvcG1lbnQgY2VydGlmaWNhdGVzMB4XDTIwMTAxNDE2MTA0NFoX
DTMwMTAxMjE2MTA0NFowSjELMAkGA1UEBhMCVVMxGDAWBgNVBAoMD19EZXZlbG9w
bWVudCBDQTEhMB8GA1UEAwwYRGV2ZWxvcG1lbnQgY2VydGlmaWNhdGVzMIIBIjAN
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzpZt/KQzyM+eJW336oeZFb+wqrBQ
gcTPttoRIbaVKvxsGUiXcf1RaYrn9KLfNn3J6HGv6s1gO/sPooNch53dq8pkqPtZ
VQUn66A/0FIMvkBd0Y/zTb5n4eTFwJcrn1xhYiBEODkkr+4c6YAAzDGg2ngY6gay
0womr6C72NDOFQCXVewr+KkXi6DWqIF1cE4hBZ0i97ASMCkWGL/3+cotzmbdBkE1
@BenMorel
BenMorel / bootstrap.php
Last active Feb 9, 2021
The first lines of any decent PHP quick script
View bootstrap.php
<?php
declare(strict_types=1);
error_reporting(E_ALL);
set_error_handler(function ($severity, $message, $file, $line) {
if ((error_reporting() & $level) === 0) {
return false;
}
@BenMorel
BenMorel / dump.php
Created Nov 23, 2020
Example of using the benmorel/smartdump PHP API instead of the CLI
View dump.php
<?php
/**
* Example valid with benmorel/smartdump~0.2.0
*
* https://github.com/BenMorel/smartdump
*/
use BenMorel\SmartDump\Configuration\DumpConfiguration;
use BenMorel\SmartDump\Configuration\TargetTable;
View changelog-from-github-api.php
<?php
/**
* This script downloads the list of releases of a project via the GitHub API,
* and generates a changelog out of it.
*
* Example, to generate a changelog for brick/math:
*
* php changelog-from-github-api.php brick/math > CHANGELOG.md
*/