From f64a8e0e9d8a28285eeb9da6ac02fed5eb496ac3 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 17 Jan 2018 12:43:44 -0500 Subject: [PATCH] Code style updates --- composer.json | 2 +- src/Config.php | 12 ++++++------ src/Friend.php | 39 +++++++++++++++++++++++++++++---------- src/Type/StringType.php | 5 +++-- src/View/HtmlView.php | 4 ++-- src/View/HttpView.php | 19 +++++++++++-------- 6 files changed, 52 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index bac3e23..6a7526d 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", "build": "robo build", "docs": "cd build && ../vendor/bin/phpdox && cd ..", - "phpstan": "phpstan analyse src tests", + "phpstan": "phpstan analyse -l 7 src ", "test": "phpunit" }, "suggest": { diff --git a/src/Config.php b/src/Config.php index 5245533..2375288 100644 --- a/src/Config.php +++ b/src/Config.php @@ -32,7 +32,7 @@ class Config implements ConfigInterface { * * @var ArrayType */ - protected $map = []; + protected $map; /** * Constructor @@ -53,7 +53,7 @@ class Config implements ConfigInterface { */ public function get($key) { - if (is_array($key)) + if (\is_array($key)) { return $this->map->getDeepKey($key); } @@ -67,9 +67,9 @@ class Config implements ConfigInterface { * @param string|array $key * @return void */ - public function delete($key) + public function delete($key): void { - if (is_array($key)) + if (\is_array($key)) { $this->map->setDeepKey($key, NULL); } @@ -90,7 +90,7 @@ class Config implements ConfigInterface { */ public function set($key, $value): ConfigInterface { - if (is_array($key)) + if (\is_array($key)) { $this->map->setDeepKey($key, $value); } @@ -100,7 +100,7 @@ class Config implements ConfigInterface { } else { - throw new InvalidArgumentException("Key must be integer, string, or array, and cannot be empty"); + throw new InvalidArgumentException('Key must be integer, string, or array, and cannot be empty'); } return $this; diff --git a/src/Friend.php b/src/Friend.php index 0b722cb..ccba8ba 100644 --- a/src/Friend.php +++ b/src/Friend.php @@ -29,27 +29,27 @@ class Friend { /** * Object to create a friend of - * @var object + * @var mixed */ private $_friend_; /** * Reflection class of the object - * @var object + * @var \ReflectionClass */ private $_reflect_; /** * Create a friend object * - * @param object $obj + * @param mixed $obj * @throws InvalidArgumentException */ public function __construct($obj) { - if ( ! is_object($obj)) + if ( ! \is_object($obj)) { - throw new InvalidArgumentException("Friend must be an object"); + throw new InvalidArgumentException('Friend must be an object'); } $this->_friend_ = $obj; @@ -64,15 +64,30 @@ class Friend { */ public function __get(string $key) { - if ($this->_reflect_->hasProperty($key)) + if ($this->__isset($key)) { $property = $this->_get_property($key); - return $property->getValue($this->_friend_); + + if ($property !== NULL) + { + return $property->getValue($this->_friend_); + } } return NULL; } + /** + * See if a property exists on the friend + * + * @param string $name + * @return bool + */ + public function __isset(string $name): bool + { + return $this->_reflect_->hasProperty($name); + } + /** * Set a friend's property * @@ -82,10 +97,14 @@ class Friend { */ public function __set(string $key, $value) { - if ($this->_reflect_->hasProperty($key)) + if ($this->__isset($key)) { $property = $this->_get_property($key); - $property->setValue($this->_friend_, $value); + + if ($property !== NULL) + { + $property->setValue($this->_friend_, $value); + } } } @@ -115,7 +134,7 @@ class Friend { * @param string $name * @return ReflectionProperty|null */ - private function _get_property(string $name) + private function _get_property(string $name): ?ReflectionProperty { try { diff --git a/src/Type/StringType.php b/src/Type/StringType.php index af8cbe5..84ac330 100644 --- a/src/Type/StringType.php +++ b/src/Type/StringType.php @@ -28,12 +28,13 @@ class StringType extends Stringy { * such as camelCase, PascalCase, kebab-case, or snake_case. * * @param string $strToMatch + * @throws \InvalidArgumentException * @return boolean */ public function fuzzyCaseMatch(string $strToMatch): bool { - $firstStr = StringType::create($this->str)->dasherize($this->str)->__toString(); - $secondStr = StringType::create($strToMatch)->dasherize()->__toString(); + $firstStr = self::create($this->str)->dasherize()->__toString(); + $secondStr = self::create($strToMatch)->dasherize()->__toString(); return $firstStr === $secondStr; } diff --git a/src/View/HtmlView.php b/src/View/HtmlView.php index 71846c7..68cb343 100644 --- a/src/View/HtmlView.php +++ b/src/View/HtmlView.php @@ -26,7 +26,7 @@ class HtmlView extends HttpView { /** * HTML generator/escaper helper * - * @var Aura\Html\HelperLocator + * @var \Aura\Html\HelperLocator */ protected $helper; @@ -62,7 +62,7 @@ class HtmlView extends HttpView { $data['container'] = $this->container; ob_start(); - extract($data); + extract($data, \EXTR_SKIP); include_once $path; $buffer = ob_get_contents(); ob_end_clean(); diff --git a/src/View/HttpView.php b/src/View/HttpView.php index 0bb864b..32c14f8 100644 --- a/src/View/HttpView.php +++ b/src/View/HttpView.php @@ -38,14 +38,15 @@ class HttpView extends BaseView { * * @param string $url * @param int $code + * @throws \InvalidArgumentException * @return void */ - public function redirect(string $url, int $code) + public function redirect(string $url, int $code): void { ob_start(); - $message = $this->response->getReasonPhrase($code); $this->setStatusCode($code); - $this->response->withHeader('Location', $url); + $message = $this->response->getReasonPhrase(); + $this->response = $this->response->withHeader('Location', $url); if (PHP_SAPI !== 'cli') { @@ -75,9 +76,10 @@ class HttpView extends BaseView { * any attempt to call again will result in a DoubleRenderException. * * @throws DoubleRenderException + * @throws \InvalidArgumentException * @return void */ - public function send() + public function send(): void { $this->output(); } @@ -86,22 +88,23 @@ class HttpView extends BaseView { * Send the appropriate response * * @throws DoubleRenderException + * @throws \InvalidArgumentException * @return void */ - protected function output() + protected function output(): void { if ($this->hasRendered) { throw new DoubleRenderException(); } - $this->response = $this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8") + $this->response = $this->response + ->withHeader('Content-type', "{$this->contentType};charset=utf-8") ->withHeader('X-Content-Type-Options', 'nosniff') ->withHeader('X-XSS-Protection', '1;mode=block') ->withHeader('X-Frame-Options', 'SAMEORIGIN'); - $sender = new SapiEmitter($this->response); - $sender->emit($this->response); + (new SapiEmitter())->emit($this->response); $this->hasRendered = TRUE; }