Skip to content

Instantly share code, notes, and snippets.

View szyku's full-sized avatar

Szymon Szymański szyku

  • Poland
View GitHub Profile
@szyku
szyku / fopenexample.php
Created April 28, 2019 10:14
fopen example
<?php
$handler = @fopen(join(DIRECTORY_SEPARATOR, [__DIR__, 'credit_cards.txt']), "r");
if ($handler) {
return CreditCardNumberExtractor::createFromHandler($handler);
}
/* ... */
@szyku
szyku / resultobjectusage.php
Created April 28, 2019 10:11
Result Object Usage
<?php
$operation = File::openForRead(join(DIRECTORY_SEPARATOR, [__DIR__, 'credit_cards.txt']));
if ($operation->isSuccessful) {
return CreditCardNumberExtractor::createFromHandler($operation->payload);
}
/* ... */
@szyku
szyku / resultobject.php
Created April 28, 2019 10:09
Result Object
<?php
final class ResultObject
{
public $isSuccessful;
public $payload = null;
private function __construct(bool $isSuccessful, $payload = null)
{
$this->isSuccessful = $isSuccessful;
$this->payload = $payload;
@szyku
szyku / exception3.php
Last active June 10, 2019 09:39
Better reason
<?php
if ($response->getStatusCode() >= 400) {
$code = $this->extractErrorCode($response->getBody());
switch ($code) {
case FailReasons::NO_TAGS:
$msg = 'The document has not enough signature tags';
break;
//...
@szyku
szyku / exception2.php
Last active June 10, 2019 09:39
Better exception names
<?php
//....
$action = DocumentSigningAction::create($doc);
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $action->execute();
if ($response->getStatusCode() >= 500) {
throw new SigningServiceUnavailable();
}
@szyku
szyku / calculator.php
Last active June 10, 2019 09:38
Calculator Exception Usage
<?php
try {
$loanSchedule = $calculator->calculate($loanDeptorRequest);
} catch (CalculatorException $e) {
// log the event or sth else
}
@szyku
szyku / naive3.php
Created April 14, 2019 16:00
naivefopen3
<?php
class XYZ {
// ...
public static function openForRead(string $id): ?SplFileObject
{
$fileInfo = new SplFileInfo($id);
try {
return $fileInfo->openFile('r');
} catch (\Exception $e) {
return null;
@szyku
szyku / naive2.php
Created April 14, 2019 15:43
naivefopen2
<?php
class XYZ {
// ...
public static function openForRead(string $id): ?resouce
{
$resource = @fopen($id, 'r');
return $resource ? $resource : null;
}
}
@szyku
szyku / naive1.php
Created April 14, 2019 15:33
naive1
<?php
class XYZ {
// ...
public function openForRead($id)
{
return @fopen($id, 'r');
}
}
@szyku
szyku / oop-ex-1.java
Created January 26, 2019 18:53
Example for OOP #1
public class Person {
String name;
String lastName;
int age;
}