Skip to content

Instantly share code, notes, and snippets.

View alcaeus's full-sized avatar

Andreas Braun alcaeus

View GitHub Profile
@alcaeus
alcaeus / in_array_vs_isset_vs_array_key_exists.php
Created July 14, 2017 08:39
Performance comparision: in-array vs. isset vs. array_key_exists
<?php declare(strict_types = 1);
function testPerformance($name, Closure $closure, $runs = 1000000)
{
$start = microtime(true);
for (; $runs > 0; $runs--)
{
$closure();
}
$end = microtime(true);
> db.foo.count()
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'bar'}, upsert: true})
null
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'baz'}, upsert: true})
{ "_id" : ObjectId("5922e0b18c00e100490e9108"), "foo" : "bar" }
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'foobar'}, upsert: true, new: true})
{ "_id" : ObjectId("5922e0b18c00e100490e9108"), "foo" : "foobar" }
@alcaeus
alcaeus / README.md
Created April 10, 2017 10:17
HTTP/2 with PHP internal webserver

Demo to show PHP's internal webserver failing to handle HTTP/2 requests with cURL

  1. Install dependencies: composer install
  2. Start the webserver: php -S localhost:8989
  3. Run the client: php client.php

Expected behavior: request is completed successfully.

Actual behavior:

@alcaeus
alcaeus / embedded.php
Created February 28, 2017 10:16
Relationship from child document to parent
<?php
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
/**
* @ODM\EmbeddedDocument
* @ODM\HasLifecycleCallbacks
*/
class EmbeddedDocument
{
@alcaeus
alcaeus / FooImplementation.php
Last active November 25, 2016 16:01
Extending interfaces
<?php
declare(strict_types = 1);
class FooImplementation implements FooInterface
{
// Note: this throws an error in PHP 7.1:
// Declaration of FooImplementation::doSomething(): FooImplementation must be compatible with FooInterface::doSomething(): FooInterface
public function doSomething(): FooImplementation
{
@alcaeus
alcaeus / Builder.php
Last active November 8, 2016 15:00
Builder interfaces for MongoDB query builders in a backwards compatible way
<?php
namespace Doctrine\MongoDB\Builders\Query;
class Builder implements BuilderInterface, MongoDB32BuilderInterface {
// ...
}
@alcaeus
alcaeus / README.md
Last active October 13, 2016 06:24
Invalid command cursor on listCollections with a sharded cluster in MongoDB 3.2

Install mongo-orchestration and start a sharded cluster:

mongo-orchestration -b 127.0.0.1 -p 8889 --no-fork start
curl -XPUT http://localhost:8889/v1/sharded_clusters/myCluster --data @configurations/sharded_clusters/clean.json

Then, run the included PHP script:

$ php invalid-cursor-listCollections.php
@alcaeus
alcaeus / Dockerfile
Last active October 11, 2016 11:23
Docker file rename problem demonstration
FROM php:7.0-cli
COPY . /usr/src
WORKDIR /usr/src
RUN mkdir -p existing-dir
RUN touch existing-dir/test
RUN cp -r existing-dir existing-dir-2
CMD [ "php", "./rename.php" ]
class ProxiedProperty
{
/** @var string */
private $name;
/** @var \ReflectionProperty */
private $reflectionProperty;
/** @var object */
private $owner;
<?php
class Foo
{
/**
* @return self
*/
public function foo()
{
return $this;