I don’t know about you, but one of the first things that I learned in software engineering, when I’d mastered the basics, was to never use magic numbers in code.
If the term is not familiar, it’s:
A unique value with unexplained meaning or multiple occurrences which could (preferably) be replaced with a named constant
A good example is when integrating against or building APIs; or when building PSR-15-compliant code. It’s quite common that you’ll be returning responses, which involve setting an HTTP status code, or writing code that branches based on the HTTP status code that a response is sent with.
For example, you might send a permanent redirect response with an HTTP 302 or a temporary redirect with an HTTP 301. Or, you might return an HTTP 404 if an item could not be found, or an HTTP 410 if it can no longer be found, but was once available.
<?php
declare(strict_types=1);
namespace SimpleUserManager\Handler;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
final readonly class UserProfileHandler implements RequestHandlerInterface
{
public function __construct(private TemplateRendererInterface $renderer)
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
if (! array_key_exists('user_id', $request->getParsedBody())) {
return new RedirectResponse("/", 301);
}
return new HtmlResponse($this->renderer->render(
'sum-app::user-profile',
[]
));
}
}
if (! array_key_exists('user_id', $request->getParsedBody())) {
return new RedirectResponse("/", \PH7\JustHttp\StatusCode\StatusCode::MOVED_PERMANENTLY);
}
Are you tired of hearing how "simple" it is to deploy apps with Docker Compose, because your experience is more one of frustration? Have you read countless blog posts and forum threads that promised to teach you how to deploy apps with Docker Compose, only for one or more essential steps to be missing, outdated, or broken?