Skip to content

Instantly share code, notes, and snippets.

View mikeschinkel's full-sized avatar

Mike Schinkel mikeschinkel

View GitHub Profile
@mikeschinkel
mikeschinkel / Type.php
Created October 5, 2021 17:28
A reimagining of Sebastian Bergmann's Type.php if PHP had a "function list" declaration.
<?php declare(strict_types=1);
/*
* This file is part of sebastian/type.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Type;
@mikeschinkel
mikeschinkel / _readme.md
Last active April 6, 2022 17:33
Potential approach for streamlined error handling in Go

Proposal for Streamlined Error Handling in Go

This is a proposal for a way to simplify the error handling in Go.

The idea is to use attempt{} and recover{} blocks to separate the happy path from the error handling. NOTE: this is NOT exception handling as you would see in Java or PHP. More on why below.

In this proposal Go would allow the developer to omit capturing the last returned variable from a function call if and only if that call was within a attempt{} block. The Go compiler would handle providing access to the error instance via the geterror() function within the recover{} block if any function returns a non nil error as its last omitted parameter, i.e.:

attempt{
 data:= GetData()
@mikeschinkel
mikeschinkel / example.php
Last active July 27, 2021 12:52
How we might be able to move to using parameterized queries in WordPress core
<?php
// Put this in wp-config.php
define('PREPARED_SQL_REQUIRED',true);
// A site builder who wants to use prepared statements and parameterized queries
// could run these in various hooks before $wpdb->query() below is run.
$question_fragment = $wpdb->prepare( '`question_id` = %d', $question_id );
$answer_fragment = $wpdb->prepare( '`answer_name` = %s', $new_answer );
$wpdb->compose('UPDATE %s polls SET vote = vote+1 WHERE %s AND %s',
@mikeschinkel
mikeschinkel / make_literal.php
Created June 26, 2021 12:55
A make_literal() function to get around functions that use is_literal() over-zealously
<?php
$safe_var = 'all your base they belong to us';
file_put_contents('/tmp/exploit.txt',$safe_var );
// imagine lots of stuff going on here...
$safe_var = file_get_contents('/tmp/exploit.txt');
function make_literal(string $non_literal):string {
$literal = '';
@mikeschinkel
mikeschinkel / try-break.php
Last active March 30, 2021 10:40
Illustrating the try-break pattern in action for PHP 8.x — hopefully to see PHP > 8.0 allow replacing `do{...}while(false};` with a naked `try{}`
<?php
/**
* This shows our try-break example in action.
*/
function main() {
do {
$result = example(0,"hello");
if ($result->is_error() ) {
$result->print_error();
@mikeschinkel
mikeschinkel / _main.php
Last active March 16, 2021 03:04
Strawman psuedo-code for set of potential autoloader related classes for PHP core for proposing to internals
<?php
use PHP\Autoload;
use PHP\ClassmapLoader;
use PHP\CallableLoader;
Autoload::set_loader(new ClassmapLoader([
"Foo" => "path/to/Foo.php",
"Bar" => "another/path/to/Bar.php",
"Baz" => "yet/another/path/to/Baz.php",
@mikeschinkel
mikeschinkel / go.mod
Last active February 25, 2021 20:01
Simple GoLang App to demonstrate Mountebank's inability to support HTTP CONNECT method.
module proxy-client
go 1.15
require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
)
@mikeschinkel
mikeschinkel / 1.attributes-with-use.php
Last active August 18, 2020 20:02
Hypothetical example of using use statement for attributes
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
@mikeschinkel
mikeschinkel / 0.readme.md
Last active May 17, 2020 23:18
Refactoring Pantheon_Sessions::initialize_session_override() using do{...}while(false);

Example use of do {...} while(false)

See this gist for background.

This example comes from my work here when looking for a solution for this issue related to this WordPress plugin.

I recognized I could make it clearer using the do {...} while(false) pattern so I wanted to document here.

Note:

@mikeschinkel
mikeschinkel / Apis.php
Last active May 14, 2020 21:23
PHP Examples that could benefit from Try...Break (method excerpts from real production code)
<?php
class Apis {
static function JsonGET( string $api_url, array $args = array() ) {
do {
$wp_error = null;
$args = wp_parse_args( $args, array(
'response_type' => ARRAY_A,
) );