Skip to content

Instantly share code, notes, and snippets.

View azjezz's full-sized avatar
😶

Saif Eddin Gmati azjezz

😶
View GitHub Profile
@Ocramius
Ocramius / handling-optional-input-fields-with-type-safe-abstractions.md
Last active March 17, 2023 12:06
Handling optional input parameters in PHP with `vimeo/psalm` and `azjezz/psl`

Handling optional input parameters in PHP with vimeo/psalm and azjezz/psl

I had an interesting use-case with a customer for which I provide consulting services: they needed multiple fields to be marked as "optional".

Example: updating a user

We will take a CRUD-ish example, for the sake of simplicity.

For example, in the following scenario, does a null $description mean "remove the description",

@silverbackdan
silverbackdan / OrSearchFilter.php
Last active July 12, 2023 09:30
The API Platform filter will allow you to pass a query `or[field1,field2]=value` to search multiple fields for a search value. It works by using the standard API Platform SearchFilter as a basis, and wrapping all of your OR queried inside a single AND query so you can combine with other where bys
<?php
declare(strict_types=1);
namespace App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Entity\Term;
@peterjaap
peterjaap / lighthouse-locally.md
Last active December 15, 2020 09:26
Run Lighthouse locally

Run Lighthouse locally

You only need Docker installed. Add to ~/.zshrc, or ~/.bashrc, or whatever you're using;

lh() {
  mkdir -p lighthouse && \
  docker run --rm -it -v "$PWD":/usr/src -w /usr/src markhobson/node-chrome /bin/bash -c "npm i -g lighthouse && lighthouse --enable-error-reporting --chrome-flags=\"--headless --no-sandbox\" $1 --output html --output-path ./lighthouse/index.html" && \
  open lighthouse/index.html
}
@Ocramius
Ocramius / compiler-pass.php
Last active March 16, 2020 07:07
Compiler pass to declare a repository as service for each declared doctrine entity in a symfony application
<?php
final class RegisterRepositoriesForAllEntitiesPass implements CompilerPassInterface
{
/** {@inheritdoc} */
public function process(ContainerBuilder $container)
{
/** @var MappingDriver $metadata */
$metadata = $container->get($this->mappingDriverServiceName());
$entityManagerReference = new Reference('doctrine.orm.entity_manager');
@bagder
bagder / http3.php
Last active July 26, 2023 14:33
HTTP/3 with PHP/CURL
if (!defined('CURL_HTTP_VERSION_3')) {
define('CURL_HTTP_VERSION_3', 30);
}
$ch = curl_init("https://cloudflare-quic.com/");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3);
curl_exec($ch);
@krakjoe
krakjoe / example.php
Last active October 11, 2023 19:07
Executor Service with Parallel and Channels
<?php
use \parallel\{Runtime, Channel};
class ExecutorService {
public function __construct(int $workers, string $channel = __CLASS__, int $backlog = Channel::Infinite) {
if ($backlog == 0) {
/*
* execute() will block until a worker is ready
*/
# sleepy flower girl
(◡ ‿ ◡ ✿)
# y u no
ლ(ಠ益ಠლ)
# smiling breasts
(^人^)
# flipping tables
@sallar
sallar / console-example.php
Created March 27, 2013 19:44
PHP Colored CLI Output Script.
<?php
// Output screenshot:
// http://cl.ly/NsqF
// -------------------------------------------------------
include_once 'console.php';
// ::log method usage
// -------------------------------------------------------
Console::log('Im Red!', 'red');
@nikic
nikic / objects_arrays.md
Last active April 12, 2024 17:05
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't: