From b426e42e4fcfb8c932a4aee131434fce3a80a011 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Jan 2024 15:46:41 -0400 Subject: [PATCH] Added typehints to View::class --- flight/template/View.php | 49 +++++++++++++++------------------------- tests/ViewTest.php | 2 +- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index 21963ee..6207288 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -17,24 +17,24 @@ namespace flight\template; */ class View { - /** @var string Location of view templates. */ - public $path; + /** Location of view templates. */ + public string $path; - /** @var string File extension. */ - public $extension = '.php'; + /** File extension. */ + public string $extension = '.php'; /** @var array View variables. */ - protected $vars = []; + protected array $vars = []; - /** @var string Template file. */ - private $template; + /** Template file. */ + private string $template; /** * Constructor. * * @param string $path Path to templates directory */ - public function __construct($path = '.') + public function __construct(string $path = '.') { $this->path = $path; } @@ -42,11 +42,9 @@ class View /** * Gets a template variable. * - * @param string $key - * * @return mixed Variable value or `null` if doesn't exists */ - public function get($key) + public function get(string $key) { return $this->vars[$key] ?? null; } @@ -58,7 +56,7 @@ class View * @param mixed $value Value * @return $this */ - public function set($key, $value = null) + public function set($key, $value = null): self { if (\is_iterable($key)) { foreach ($key as $k => $v) { @@ -74,11 +72,9 @@ class View /** * Checks if a template variable is set. * - * @param string $key - * * @return bool If key exists */ - public function has($key) + public function has(string $key): bool { return isset($this->vars[$key]); } @@ -86,11 +82,9 @@ class View /** * Unsets a template variable. If no key is passed in, clear all variables. * - * @param ?string $key - * * @return $this */ - public function clear($key = null) + public function clear(?string $key = null): self { if (null === $key) { $this->vars = []; @@ -107,10 +101,9 @@ class View * @param string $file Template file * @param ?array $data Template data * - * @return void * @throws \Exception If template not found */ - public function render($file, $data = null) + public function render(string $file, ?array $data = null): void { $this->template = $this->getTemplate($file); @@ -136,7 +129,7 @@ class View * * @return string Output of template */ - public function fetch($file, $data = null) + public function fetch(string $file, ?array $data = null): string { \ob_start(); @@ -152,7 +145,7 @@ class View * * @return bool Template file exists */ - public function exists($file) + public function exists(string $file): bool { return \file_exists($this->getTemplate($file)); } @@ -164,7 +157,7 @@ class View * * @return string Template file location */ - public function getTemplate($file) + public function getTemplate(string $file): string { $ext = $this->extension; @@ -188,20 +181,14 @@ class View * * @return string Escaped string */ - public function e($str) + public function e(string $str): string { $value = \htmlentities($str); echo $value; return $value; } - /** - * @param string $path An unnormalized path. - * @param string $separator Path separator. - * - * @return string Normalized path. - */ - protected static function normalizePath($path, $separator = DIRECTORY_SEPARATOR) + protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string { return \str_replace(['\\', '/'], $separator, $path); } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index f83e0c5..896b129 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -124,7 +124,7 @@ class ViewTest extends PHPUnit\Framework\TestCase public function testNormalizePath(): void { $viewMock = new class extends View { - public static function normalizePath($path, $separator = DIRECTORY_SEPARATOR) + public static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string { return parent::normalizePath($path, $separator); }