Skip to content

Instantly share code, notes, and snippets.

View Chemaclass's full-sized avatar
💡
🏗️

Jose Maria Valera Reales Chemaclass

💡
🏗️
View GitHub Profile
@Chemaclass
Chemaclass / moby-dick.txt
Last active March 22, 2021 19:44
Moby Dick (txt)
THE WORKS OF
HERMAN MELVILLE
STANDARD EDITION
VOLUME
VII
@Chemaclass
Chemaclass / MyBusinessLogicTest.php
Last active June 20, 2021 11:05
Unit testing effectively - Test example (Part II)
<?php
declare(strict_types=1);
namespace CompanyTest\Domain;
use PHPUnit\Framework\TestCase;
final class MyBusinessLogicTest extends TestCase
{
@Chemaclass
Chemaclass / MyBusinessLogic.php
Last active August 9, 2020 11:08
Unit testing effectively - Logic example (Part I)
<?php declare(strict_types=1);
namespace Company\Domain;
final class MyBusinessLogic
{
private DependencyInterface $dependencyInterface;
private ConcreteDependency $concrete;
public function __construct(
@Chemaclass
Chemaclass / TestLoggerSpy.php
Created June 8, 2020 03:41
An example using a Test Double Spy
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
interface Logger {
public function log(string $message): void;
}
final class LoggerSpy implements Logger {
public array $messages = [];
@Chemaclass
Chemaclass / CachedRemoteSiteManager.php
Last active April 17, 2020 08:10
This is the code that I used to verify locally my answer for a concret stackoverflow question
<?php declare(strict_types=1);
# https://stackoverflow.com/a/61230034/3454593
// Usage example:
$cacheManager = new CachedRemoteSiteManager(
new class implements CacheNormalizer {
public function normalize(string $text): string {
return substr($text, 17, 2);
}
},
@Chemaclass
Chemaclass / YieldTraining1.php
Last active April 17, 2020 08:10
This is the code that I used to verify locally my answer for a concret stackoverflow question
<?php declare(strict_types=1);
# https://stackoverflow.com/a/58767497/3454593
// Usage example:
$postWithImage = new PostWithImage(
$chaptersUrlApi = 'https://httpbin.org/get',
$imagesUrlApi = 'https://httpbin.org/get?{$key}={$value}'
);
foreach ($postWithImage->generate(20) as $post) {
@Chemaclass
Chemaclass / EdiFileGenerator.php
Last active October 17, 2019 13:57
EdiFileGenerator
<?php declare(strict_types=1);
final class EdiFileGenerator
{
private const MESSAGE_TEMPLATE = <<<STR
UNH+%d+IFTMIN:S:93A:UN:PN001'
BGM+340+0035800000000819%d+9'
DTM+10:20191002:102'
TSR+19+A2'
CNT+7:0.1:KGM'
@Chemaclass
Chemaclass / StripTagsWithArrayOfTagNames.php
Created September 22, 2019 12:55
Docu - Strip_tags() with array of tag names
<?php
// Instead of:
strip_tags($str, '<a><p>')
// You can now write:
strip_tags($str, ['a', 'p'])
@Chemaclass
Chemaclass / GeneratorDelegation.php
Last active September 23, 2019 12:08
Docu - Generator delegation
<?php
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
@Chemaclass
Chemaclass / GeneratorReturnExpressions.php
Created September 21, 2019 22:56
Docu - Generator Return Expressions
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, ' ';
}