Skip to content

Instantly share code, notes, and snippets.

View tsh-code's full-sized avatar

TSH code sharing tsh-code

View GitHub Profile
@tsh-code
tsh-code / naive1.php
Created June 10, 2019 09:37 — forked from szyku/naive1.php
naive1
<?php
class XYZ {
// ...
public function openForRead($id)
{
return @fopen($id, 'r');
}
}
@tsh-code
tsh-code / naive2.php
Created June 10, 2019 09:37 — forked from szyku/naive2.php
naivefopen2
<?php
class XYZ {
// ...
public static function openForRead(string $id): ?resouce
{
$resource = @fopen($id, 'r');
return $resource ? $resource : null;
}
}
@tsh-code
tsh-code / naive3.php
Created June 10, 2019 09:38 — forked from szyku/naive3.php
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;
@tsh-code
tsh-code / calculator.php
Created June 10, 2019 09:38 — forked from szyku/calculator.php
Calculator Exception Usage
<?php
try {
$loanSchedule = $calculator->calculate($loanDeptorRequest);
} catch (CalculatorException $e) {
// log the event or sth else
}
@tsh-code
tsh-code / exception1.php
Created June 10, 2019 09:38 — forked from szyku-tsh/exception1.php
First attempt
<?php
//....
$action = DocumentSigningAction::create($doc);
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $action->execute();
if ($response->getStatusCode() !== 200) {
throw new SigningException("Could not sign document");
}
@tsh-code
tsh-code / exception2.php
Created June 10, 2019 09:39 — forked from szyku/exception2.php
Better exception names
<?php
//....
$action = DocumentSigningAction::create($doc);
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $action->execute();
if ($response->getStatusCode() >= 500) {
throw new SigningServiceUnavailable();
}
@tsh-code
tsh-code / exception3.php
Last active June 10, 2019 09:39 — forked from szyku/exception3.php
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;
//...
@tsh-code
tsh-code / resultobject.php
Created June 10, 2019 09:40 — forked from szyku/resultobject.php
Result Object
<?php
final class ResultObject
{
public $isSuccessful;
public $payload = null;
private function __construct(bool $isSuccessful, $payload = null)
{
$this->isSuccessful = $isSuccessful;
$this->payload = $payload;
@tsh-code
tsh-code / fopenexample.php
Created June 10, 2019 09:40 — forked from szyku/fopenexample.php
fopen example
<?php
$handler = @fopen(join(DIRECTORY_SEPARATOR, [__DIR__, 'credit_cards.txt']), "r");
if ($handler) {
return CreditCardNumberExtractor::createFromHandler($handler);
}
/* ... */
@tsh-code
tsh-code / resultobjectusage.php
Created June 10, 2019 09:44 — forked from szyku/resultobjectusage.php
Result Object Usage
<?php
$operation = File::openForRead(join(DIRECTORY_SEPARATOR, [__DIR__, 'credit_cards.txt']));
if ($operation->isSuccessful) {
return CreditCardNumberExtractor::createFromHandler($operation->payload);
}
/* ... */