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 / 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 / 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 / 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 / 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 / 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 / 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');
}
}