Skip to content

Instantly share code, notes, and snippets.

View gskema's full-sized avatar

Gytis Šk. gskema

  • Kaunas, Lithuania
View GitHub Profile
@gskema
gskema / NestedIterator.php
Last active May 3, 2021 14:32
PHP Nested Iterator
<?php
namespace Iterator;
use ArrayIterator;
use Iterator;
/**
* Takes a set of iterators [iterator1, iterator2, iterator3, ...]
* and iterates in an equivalent way to:
@gskema
gskema / ContainerServiceId.php
Created April 22, 2020 07:09
Get service ID from Symfony container by object service
<?php
namespace Vrt\UtilsBundle\Service;
use RuntimeException;
use Symfony\Component\DependencyInjection\Container;
// AppKernel::initializeContainer()
// if ('dev' === $this->environment) {
// ContainerServiceId::setContainer($this->container);
@gskema
gskema / permutation_iterator.php
Created February 14, 2019 12:35
PHP Permutation Iterator
<?php
class PermutationIterator implements Iterator
{
/** @var array */
protected $items;
/** @var int */
protected $slots;
@gskema
gskema / noinspection.php
Last active April 2, 2024 10:39
PhpStorm @noinspection list of all tags
<?php
/** @noinspection ? */
// PhpUndefinedGotoLabelInspection Undefined goto label
// PhpUndefinedVariableInspection Undefined variable
// PhpUndefinedMethodInspection Undefined method
// PhpUndefinedNamespaceInspection Undefined namespace
// PhpUndefinedClassInspection Undefined class
// PhpUndefinedFunctionInspection Undefined function
@gskema
gskema / auto_test_clone.php
Last active June 8, 2018 21:20
Automatic PHPUnit test for object cloning ::__clone()
<?php
use PHPUnit\Framework\TestCase;
class CloneTest extends TestCase
{
/**
* @dataProvider testClone
*
* @param object $obj0
@gskema
gskema / GetterSetterTestingTrait.php
Last active March 9, 2018 20:04
Automatically tests basic setters/getters, assumes your test namespace matches the tested class namespaces. Tests setter with 1 parameter only.
<?php
namespace Acme;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use ReflectionType;
use RuntimeException;
@gskema
gskema / obscure.php
Created April 24, 2017 10:42
Obscure arbitrary security-sensitive string in PHP
<?php
/**
* Obscures sensitive string (like a security token),
* but preserves minimal information about letters, digits, spaces and unknown characters.
*
* @param string $string
*
* @return string
*/
@gskema
gskema / Banana.php
Last active April 14, 2017 08:59
PHPUnit setMethods explained by an example
<?php
class Banana
{
public function a()
{
return 1;
}
public function b()
@gskema
gskema / BulkInsertQuery.php
Last active November 4, 2023 10:56
PHP PDO / Doctrine DBAL bulk insert query
<?php
namespace YourApp\Repository\Query;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Identifier;
/**
* Class BulkInsertQuery
*
@gskema
gskema / Object1Test.php
Last active February 21, 2017 10:23
PHPUnit object method testing pattern/snippet/boilerplate
<?php
class Object1
{
protected $property1;
public function __construct($property1)
{
$this->property1 = $property1;
}