Refactored classes to take adavantage of constructor property promotion

This commit is contained in:
Chris Kankiewicz
2022-12-21 12:46:09 -07:00
parent a4f26cb37b
commit 425fb05ce9
36 changed files with 116 additions and 306 deletions

View File

@@ -23,14 +23,12 @@ return [
'app_files' => ['app', 'index.php', '.env', '.env.example', '.hidden'],
/** Array of application middlewares */
'middlewares' => function (): array {
return [
Middlewares\WhoopsMiddleware::class,
Middlewares\PruneCacheMiddleware::class,
Middlewares\CacheControlMiddleware::class,
Middlewares\RegisterGlobalsMiddleware::class,
];
},
'middlewares' => fn (): array => [
Middlewares\WhoopsMiddleware::class,
Middlewares\PruneCacheMiddleware::class,
Middlewares\CacheControlMiddleware::class,
Middlewares\RegisterGlobalsMiddleware::class,
],
/** Array of sort options mapped to their respective classes */
'sort_methods' => [

View File

@@ -8,13 +8,10 @@ use Slim\App;
class AppManager
{
/** @var Container The applicaiton container */
protected $container;
/** Create a new AppManager object. */
public function __construct(Container $container)
{
$this->container = $container;
public function __construct(
private Container $container
) {
}
/** Setup and configure the application. */

View File

@@ -8,17 +8,11 @@ use Slim\App;
class ExceptionManager
{
/** @var App The application */
protected $app;
/** @var Config The application configuration */
protected $config;
/** Create a new ExceptionManager object. */
public function __construct(App $app, Config $config)
{
$this->app = $app;
$this->config = $config;
public function __construct(
private App $app,
private Config $config
) {
}
/** Set up and configure exception handling. */

View File

@@ -7,17 +7,11 @@ use Slim\App;
class MiddlewareManager
{
/** @var App The application */
protected $app;
/** @var Config The application configuration */
protected $config;
/** Create a new MiddlwareManager object. */
public function __construct(App $app, Config $config)
{
$this->app = $app;
$this->config = $config;
public function __construct(
private App $app,
private Config $config
) {
}
/** Register application middlewares. */

View File

@@ -7,13 +7,10 @@ use Slim\App;
class RouteManager
{
/** @var App The application */
protected $app;
/** Create a new RouteManager object. */
public function __construct(App $app)
{
$this->app = $app;
public function __construct(
private App $app
) {
}
/** Register the application routes. */

View File

@@ -7,13 +7,10 @@ use DI\NotFoundException;
class Config
{
/** @var Container The application container */
protected $container;
/** Create a new Config object. */
public function __construct(Container $container)
{
$this->container = $container;
public function __construct(
private Container $container
) {
}
/**

View File

@@ -14,29 +14,13 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class DirectoryController
{
/** @var Config The application configuration */
protected $config;
/** @var Finder File finder component */
protected $finder;
/** @var Twig Twig templating component */
protected $view;
/** @var TranslatorInterface Translator component */
protected $translator;
/** Create a new IndexController object. */
public function __construct(
Config $config,
Finder $finder,
Twig $view,
TranslatorInterface $translator
private Config $config,
private Finder $finder,
private Twig $view,
private TranslatorInterface $translator
) {
$this->config = $config;
$this->finder = $finder;
$this->view = $view;
$this->translator = $translator;
}
/** Invoke the IndexController. */

View File

@@ -12,24 +12,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class FileInfoController
{
/** @var Config The application configuration */
protected $config;
/** @var CacheInterface The application cache */
protected $cache;
/** @var TranslatorInterface Translator component */
protected $translator;
/** Create a new FileInfoHandler object. */
public function __construct(
Config $config,
CacheInterface $cache,
TranslatorInterface $translator
private Config $config,
private CacheInterface $cache,
private TranslatorInterface $translator
) {
$this->config = $config;
$this->cache = $cache;
$this->translator = $translator;
}
/** Invoke the FileInfoHandler. */

View File

@@ -9,13 +9,10 @@ use Slim\Psr7\Response;
class IndexController
{
/** @var Container Application container */
protected $container;
/** Create a new IndexController object. */
public function __construct(Container $container)
{
$this->container = $container;
public function __construct(
private Container $container
) {
}
/** Invoke the IndexController. */

View File

@@ -11,21 +11,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class SearchController
{
/** @var Finder File finder component */
protected $finder;
/** @var Twig Twig templating component */
protected $view;
/** @var TranslatorInterface Translator component */
protected $translator;
/** Create a new SearchHandler object. */
public function __construct(Finder $finder, Twig $view, TranslatorInterface $translator)
{
$this->finder = $finder;
$this->view = $view;
$this->translator = $translator;
public function __construct(
private Finder $finder,
private Twig $view,
private TranslatorInterface $translator
) {
}
/** Invoke the SearchHandler. */

View File

@@ -16,29 +16,13 @@ use ZipArchive;
class ZipController
{
/** @var Config The application configuration */
protected $config;
/** @var CacheInterface The application cache */
protected $cache;
/** @var Finder The Finder Component */
protected $finder;
/** @var TranslatorInterface Translator component */
protected $translator;
/** Create a new ZipHandler object. */
public function __construct(
Config $config,
CacheInterface $cache,
Finder $finder,
TranslatorInterface $translator
private Config $config,
private CacheInterface $cache,
private Finder $finder,
private TranslatorInterface $translator
) {
$this->config = $config;
$this->cache = $cache;
$this->finder = $finder;
$this->translator = $translator;
}
/** Invoke the ZipHandler. */

View File

@@ -12,17 +12,11 @@ use Throwable;
class ErrorHandler implements ErrorHandlerInterface
{
/** @var Twig Twig templating component */
protected $view;
/** @var TranslatorInterface Translation component */
protected $translator;
/** Create a new ErrorHandler object. */
public function __construct(Twig $view, TranslatorInterface $translator)
{
$this->view = $view;
$this->translator = $translator;
public function __construct(
private Twig $view,
private TranslatorInterface $translator
) {
}
/** Invoke the ErrorHandler class. */

View File

@@ -23,17 +23,11 @@ class CacheFactory
/** @const Namespace for internal cache drivers */
protected const NAMESPACE_INTERNAL = 'app';
/** @var Container The application container */
protected $container;
/** @var Config The application configuration */
protected $config;
/** Create a new CacheFactory object. */
public function __construct(Container $container, Config $config)
{
$this->container = $container;
$this->config = $config;
public function __construct(
private Container $container,
private Config $config
) {
}
/** Initialize and return a CacheInterface. */

View File

@@ -14,27 +14,15 @@ use Symfony\Component\Finder\SplFileInfo;
class FinderFactory
{
/** @var Container The application container */
protected $container;
/** @var HiddenFiles Collection of hidden files */
protected $hiddenFiles;
/** @var Pattern|null Hidden files pattern cache */
protected $pattern;
/** @var Config The application configuration */
protected $config;
/** @var ?Pattern Hidden files pattern cache */
private ?Pattern $pattern = null;
/** Create a new FinderFactory object. */
public function __construct(
Container $container,
Config $config,
HiddenFiles $hiddenFiles
private Container $container,
private Config $config,
private HiddenFiles $hiddenFiles
) {
$this->container = $container;
$this->config = $config;
$this->hiddenFiles = $hiddenFiles;
}
/** Initialize and return the Finder component. */

View File

@@ -13,17 +13,11 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class TranslationFactory
{
/** @var Config The applicaiton configuration */
protected $config;
/** @var CacheInterface The application cache */
protected $cache;
/** Create a new TranslationFactory object. */
public function __construct(Config $config, CacheInterface $cache)
{
$this->config = $config;
$this->cache = $cache;
public function __construct(
private Config $config,
private CacheInterface $cache
) {
}
/** Initialize and return the translation component. */

View File

@@ -12,19 +12,11 @@ use Twig\TwigFunction;
class TwigFactory
{
/** @var Config The application configuration */
protected $config;
/** @var CallableResolver The callable resolver */
protected $callableResolver;
/** Create a new TwigFactory object. */
public function __construct(
Config $config,
CallableResolver $callableResolver
private Config $config,
private CallableResolver $callableResolver
) {
$this->config = $config;
$this->callableResolver = $callableResolver;
}
/** Initialize and return the Twig component. */

View File

@@ -7,13 +7,11 @@ use Tightenco\Collect\Support\Collection;
class HiddenFiles extends Collection
{
/** @inheritdoc */
protected function __construct($items = [])
{
parent::__construct($items);
}
/** {@inheritdoc} */
public static function make($items = [])
{
throw new BadMethodCallException('Method not implemented');

View File

@@ -9,13 +9,10 @@ use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
class CacheControlMiddleware
{
/** @var Config The application configuration */
protected $config;
/** Create a new CacheControlMiddleware object. */
public function __construct(Config $config)
{
$this->config = $config;
public function __construct(
private Config $config
) {
}
/** Invoke the CacheControlMiddleware class. */

View File

@@ -11,17 +11,11 @@ use Symfony\Contracts\Cache\CacheInterface;
class PruneCacheMiddleware
{
/** @var Config The application configuration */
protected $config;
/** @var CacheInterface The application cache */
protected $cache;
/** Create a new CachePruneMiddleware object. */
public function __construct(Config $config, CacheInterface $cache)
{
$this->config = $config;
$this->cache = $cache;
public function __construct(
private Config $config,
private CacheInterface $cache
) {
}
/** Invoke the CachePruneMiddleware class. */

View File

@@ -12,12 +12,9 @@ class RegisterGlobalsMiddleware
/** Array of valid theme strings. */
private const VALID_THEMES = ['dark', 'light'];
/** @var Twig Twig templating component */
protected $view;
public function __construct(Twig $view)
{
$this->view = $view;
public function __construct(
private Twig $view
) {
}
/** Invoke the RegisterGlobalsMiddleware class. */

View File

@@ -11,24 +11,12 @@ use Whoops\RunInterface;
class WhoopsMiddleware
{
/** @var RunInterface The Whoops component */
protected $whoops;
/** @var PrettyPageHandler The pretty page handler */
protected $pageHandler;
/** @var JsonResponseHandler The JSON response handler */
protected $jsonHandler;
/** Create a new WhoopseMiddleware object. */
public function __construct(
RunInterface $whoops,
PrettyPageHandler $pageHandler,
JsonResponseHandler $jsonHandler
private RunInterface $whoops,
private PrettyPageHandler $pageHandler,
private JsonResponseHandler $jsonHandler
) {
$this->whoops = $whoops;
$this->pageHandler = $pageHandler;
$this->jsonHandler = $jsonHandler;
}
/** Invoke the WhoopseMiddleware class. */

View File

@@ -5,7 +5,7 @@ namespace App;
class TemporaryFile
{
/** @var string Path to the temporary file */
protected $path;
private string $path;
/** Create a new TemporaryFile object. */
public function __construct(string $dir, string $prefix = '')

View File

@@ -7,16 +7,12 @@ use Tightenco\Collect\Support\Collection;
class Asset extends ViewFunction
{
/** @var string The function name */
protected $name = 'asset';
/** @var Config The application configuration */
protected $config;
protected string $name = 'asset';
/** Create a new Asset object. */
public function __construct(Config $container)
{
$this->config = $container;
public function __construct(
private Config $config
) {
}
/** Return the path to an asset. */

View File

@@ -8,22 +8,13 @@ use Tightenco\Collect\Support\Collection;
class Breadcrumbs extends ViewFunction
{
/** @var string The function name */
protected $name = 'breadcrumbs';
/** @var Config The application configuration */
protected $config;
/** @var string The directory separator */
protected $directorySeparator;
protected string $name = 'breadcrumbs';
/** Create a new Breadcrumbs object. */
public function __construct(
Config $config,
string $directorySeparator = DIRECTORY_SEPARATOR
private Config $config,
private string $directorySeparator = DIRECTORY_SEPARATOR
) {
$this->config = $config;
$this->directorySeparator = $directorySeparator;
}
/** Build a collection of breadcrumbs for a given path. */

View File

@@ -6,16 +6,12 @@ use App\Config as AppConfig;
class Config extends ViewFunction
{
/** @var string The function name */
protected $name = 'config';
/** @var AppConfig The application configuration */
protected $config;
protected string $name = 'config';
/** Create a new Config object. */
public function __construct(AppConfig $config)
{
$this->config = $config;
public function __construct(
private AppConfig $config
) {
}
/**

View File

@@ -4,8 +4,7 @@ namespace App\ViewFunctions;
class FileUrl extends Url
{
/** @var string The function name */
protected $name = 'file_url';
protected string $name = 'file_url';
/** Return the URL for a given path and action. */
public function __invoke(string $path = '/'): string

View File

@@ -7,16 +7,12 @@ use Symfony\Component\Finder\SplFileInfo;
class Icon extends ViewFunction
{
/** @var string The function name */
protected $name = 'icon';
/** @var Config The application configuration */
protected $config;
protected string $name = 'icon';
/** Create a new Config object. */
public function __construct(Config $config)
{
$this->config = $config;
public function __construct(
private Config $config
) {
}
/** Retrieve the icon markup for a file. */

View File

@@ -7,19 +7,12 @@ use Symfony\Contracts\Cache\CacheInterface;
class Markdown extends ViewFunction
{
/** @var string The function name */
protected $name = 'markdown';
protected string $name = 'markdown';
/** @var ParsedownExtra The markdown parser */
protected $parser;
/** @var CacheInterface */
protected $cache;
public function __construct(ParsedownExtra $parser, CacheInterface $cache)
{
$this->parser = $parser;
$this->cache = $cache;
public function __construct(
private ParsedownExtra $parser,
private CacheInterface $cache
) {
}
/** Parses a string of markdown into HTML. */

View File

@@ -8,15 +8,11 @@ use Symfony\Component\Finder\SplFileInfo;
class ModifiedTime extends ViewFunction
{
/** @var string The function name */
protected $name = 'modified_time';
protected string $name = 'modified_time';
/** @var Config The application config */
private $config;
public function __construct(Config $config)
{
$this->config = $config;
public function __construct(
private Config $config
) {
}
/** Get the modified time from a file object. */

View File

@@ -6,16 +6,12 @@ use App\Support\Str;
class ParentUrl extends ViewFunction
{
/** @var string The function name */
protected $name = 'parent_url';
/** @var string The directory separator */
protected $directorySeparator;
protected string $name = 'parent_url';
/** Create a new ParentUrl object. */
public function __construct(string $directorySeparator = DIRECTORY_SEPARATOR)
{
$this->directorySeparator = $directorySeparator;
public function __construct(
private string $directorySeparator = DIRECTORY_SEPARATOR
) {
}
/** Get the parent directory for a given path. */

View File

@@ -7,8 +7,7 @@ use Symfony\Component\Finder\SplFileInfo;
class SizeForHumans extends ViewFunction
{
/** @var string The function name */
protected $name = 'size_for_humans';
protected string $name = 'size_for_humans';
/** Get the human readable file size from a file object. */
public function __invoke(SplFileInfo $file): string

View File

@@ -6,16 +6,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class Translate extends ViewFunction
{
/** @var string The function name */
protected $name = 'translate';
/** @var TranslatorInterface The application translator */
protected $translator;
protected string $name = 'translate';
/** Create a new Translate object. */
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
public function __construct(
private TranslatorInterface $translator
) {
}
/** Retrieve a translated string by ID. */

View File

@@ -6,16 +6,12 @@ use App\Support\Str;
class Url extends ViewFunction
{
/** @var string The function name */
protected $name = 'url';
/** @var string The directory separator */
protected $directorySeparator;
protected string $name = 'url';
/** Create a new Url object. */
public function __construct(string $directorySeparator = DIRECTORY_SEPARATOR)
{
$this->directorySeparator = $directorySeparator;
public function __construct(
private string $directorySeparator = DIRECTORY_SEPARATOR
) {
}
/** Return the URL for a given path. */

View File

@@ -5,7 +5,7 @@ namespace App\ViewFunctions;
abstract class ViewFunction
{
/** @var string The function name */
protected $name = '';
protected string $name = '';
/** Get the function name. */
public function name(): string

View File

@@ -4,8 +4,7 @@ namespace App\ViewFunctions;
class ZipUrl extends Url
{
/** @var string The function name */
protected $name = 'zip_url';
protected string $name = 'zip_url';
/** Return the URL for a given path and action. */
public function __invoke(string $path = '/'): string

View File

@@ -11,7 +11,7 @@ class ViewFunctionTest extends TestCase
public function test_it_can_be_extended(): void
{
$viewFunction = new class extends ViewFunction {
protected $name = 'foo';
protected string $name = 'foo';
};
$this->assertInstanceOf(ViewFunction::class, $viewFunction);