Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* @return array
*/
function basketToViewModel(Basket $basket): array
{
$items = \array_map(function (BasketItem $item): array {
return [
'id' => $item->getProductId(),
<?php
class UserRepository
{
public function getUsers(): array
{
}
public function getUsersIncludingDeleted(): array
{
<?php
class UserRepository
{
public function getUsers(?bool $includingDeleted = false): array
{
}
}
<?php
if ($isSuccess && $user->isActive() && $httpApi->hasAccess($user)) {
// if $isSuccess is false
// other conditions will not be evaluated
}
<?php
if ($httpApi->hasAccess($user) && $user->isActive() && $isSuccess) {
// all conditions must be met in order for this condition to execute
// in case of 'and' we should keep the heaviest conditions last
// and the fastest to execute in front
}
<?php
function getTotalWithTax(Payment $payment, int $taxPercent): float
{
// descriptive variables with intermediary values for easier debugging
$paymentAmount = (int) $payment->getAmount();
$taxCoefficient = 1 + ($taxPercent / 100);
$totalWithTax = $paymentAmount * $taxCoefficient;
<?php
function getTotalWithTax(Payment $payment, int $taxPercent): float
{
// is the order correct at all?
return (float) (int) $payment->getTotal() * (1 + $taxPercent / 100);
}
<?php
function mapOrderStatusToLabel(Order $order): string
{
switch ($order->getStatus()) {
case 'complete':
return 'Completed';
case 'pending':
case 'in_transport':
<?php
function mapOrderStatusToLabel(Order $order): string
{
$label = 'unknown';
if ('complete' === $order->getStatus()) {
$label = 'Completed';
}
<?php
function mapOrderStatusToLabel(Order $order): string
{
if ($order->isComplete()) {
return 'Completed';
}
if ($order->isPending()) {
return 'In transport';