From a8674960793b9c9fb3a90b83558f972a28f7ed75 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 5 Nov 2016 15:33:30 +0100 Subject: [PATCH] type hints and style fixes --- src/array.php | 50 ++++++++++++++++++++++++------- src/cli.php | 3 +- src/container.php | 7 ++--- src/dateformat.php | 2 +- src/htmlbuilder.php | 72 +++++++++++++++++++++++++++++++++++++++++---- src/mixins.php | 8 ++--- src/shell.php | 5 ++-- 7 files changed, 118 insertions(+), 29 deletions(-) diff --git a/src/array.php b/src/array.php index 39e03e3..7978a3e 100644 --- a/src/array.php +++ b/src/array.php @@ -4,7 +4,13 @@ namespace PFF; final class Arr { - public static function get($array, $key, $default=null) + /** + * @param array $array + * @param string|integer $key + * @param mixed $default + * @return mixed + */ + public static function get(array $array, $key, $default=null) { if (array_key_exists($key, $array)) return $array[$key]; @@ -12,21 +18,37 @@ final class Arr return $default; } - public static function ensureKeyExists(&$array, $key, $default=null) + /** + * @param array $array + * @param string|integer $key + * @param mixed $default + */ + public static function ensureKeyExists(array &$array, $key, $default=null) { if (!array_key_exists($key, $array)) - return $array[$key] = $default; + $array[$key] = $default; } - public static function pushToKey(&$array, $key, $value) + /** + * @param array $array + * @param string|integer $key + * @param mixed $value + */ + public static function pushToKey(array &$array, $key, $value) { if (array_key_exists($key, $array)) - return $array[$key][] = $value; + $array[$key][] = $value; else - return $array[$key] = array($value); + $array[$key] = array($value); } - public static function pop(&$array, $key, $default=null) + /** + * @param array $array + * @param string|integer $key + * @param mixed $default + * @return mixed + */ + public static function pop(array &$array, $key, $default=null) { if (array_key_exists($key, $array)) { $value = $array[$key]; @@ -37,7 +59,11 @@ final class Arr } } - public static function flatten($array) + /** + * @param array $array + * @return array + */ + public static function flatten(array $array) { $result = array(); array_walk_recursive($array, function ($value) use (&$result) { @@ -46,7 +72,12 @@ final class Arr return $result; } - public static function pluck($array, $key) + /** + * @param array $array + * @param string|integer $key + * @return array + */ + public static function pluck(array $array, $key) { $result = array(); foreach ($array as $item) @@ -54,4 +85,3 @@ final class Arr return $result; } } - diff --git a/src/cli.php b/src/cli.php index 33d87e5..05ec6f5 100644 --- a/src/cli.php +++ b/src/cli.php @@ -11,7 +11,7 @@ abstract class CLI return $argv; if (@is_array($_SERVER['argv'])) return $_SERVER['argv']; - throw new Exception("Could not read command arguments (register_argc_argv=Off?)"); + throw new \Exception("Could not read command arguments (register_argc_argv=Off?)"); } protected abstract function commands(); @@ -86,4 +86,3 @@ abstract class CLI } } } - diff --git a/src/container.php b/src/container.php index b786268..9f1543b 100644 --- a/src/container.php +++ b/src/container.php @@ -37,13 +37,13 @@ class Container switch ($name) { case 'set': $this->values[$args[0]] = array('value', $args[1]); - break; + return $this; case 'setFunction': $this->values[$args[0]] = array('function', $args[1]); - break; + return $this; case 'setFactory': $this->values[$args[0]] = array('factory', $args[1]); - break; + return $this; default: return $this->getWithArgs($name, $args); } @@ -63,4 +63,3 @@ class Container return call_user_func_array(array(self::getInstance(), $name), $args); } } - diff --git a/src/dateformat.php b/src/dateformat.php index 8bb76a2..7a96a56 100644 --- a/src/dateformat.php +++ b/src/dateformat.php @@ -170,7 +170,7 @@ final class DateFormat $result .= ' '.$controls[6]; break; default: - throw new Exception('Bad time format'); + throw new \Exception('Bad time format'); } return $result; } diff --git a/src/htmlbuilder.php b/src/htmlbuilder.php index 32385d0..9e0a9ed 100644 --- a/src/htmlbuilder.php +++ b/src/htmlbuilder.php @@ -4,6 +4,10 @@ namespace PFF\HtmlBuilder; class Text { + /** + * @param string $text + * @return string + */ public static function escape($text) { return htmlspecialchars($text, ENT_COMPAT, 'UTF-8'); @@ -14,6 +18,11 @@ class Tag { private $name, $selfClose, $attributes, $inner=array(); + /** + * @param string $name + * @param array $attributes + * @param mixed|null $inner + */ public function __construct($name, $attributes=array(), $inner=null) { $this->name = $name; @@ -26,6 +35,9 @@ class Tag $this->append($item); } + /** + * @return Tag + */ public static function create(/*name[, attributes, [... inner]]*/) { $args = func_get_args(); @@ -34,25 +46,43 @@ class Tag return new Tag($name, $attributes, $args); } - public static function __callStatic($method, $args) + /** + * @param string $name + * @param array $args + * @return Tag + */ + public static function __callStatic($name, $args) { $attributes = array_shift($args); - return new Tag($method, $attributes, $args); + return new Tag($name, $attributes, $args); } + /** + * @param string $name + * @param string $value + * @return Tag + */ public function attr($name, $value) { $this->attributes[$name] = $value; return $this; } + /** + * @param string $name + * @return Tag + */ public function unsetAttr($name) { unset($this->attributes[$name]); return $this; } - public function attrs($attributes) + /** + * @param array $attributes dictionary + * @return Tag + */ + public function attrs(array $attributes) { foreach ($attributes as $k => $v) { if ($k == 'class') { @@ -74,7 +104,11 @@ class Tag $classes = explode(' ', $classes); return $classes; } - + + /** + * @param string $class + * @return Tag + */ public function addClass($class) { $classes = $this->getListAttribute('class'); @@ -84,6 +118,10 @@ class Tag return $this; } + /** + * @param string $class + * @return Tag + */ public function removeClass($class) { $classes = $this->getListAttribute('class'); @@ -94,6 +132,12 @@ class Tag return $this; } + /** + * @param string $name + * @param mixed $value + * @param boolean $condition + * @return Tag + */ public function toggleAttr($name, $value, $condition) { return $condition @@ -101,6 +145,11 @@ class Tag : $this->unsetAttr($name); } + /** + * @param string $class + * @param boolean $condition + * @return Tag + */ public function toggleClass($class, $condition) { return $condition @@ -108,6 +157,10 @@ class Tag : $this->removeClass($class); } + /** + * @param mixed $item + * @return Tag + */ public function append($item) { if (is_array($item)) { @@ -121,6 +174,10 @@ class Tag return $this; } + /** + * @param string $raw + * @return Tag + */ public function raw($raw) { if (is_array($raw)) { @@ -131,6 +188,9 @@ class Tag return $this; } + /** + * @return string + */ public function html() { $s = '<'.$this->name; @@ -150,9 +210,11 @@ class Tag return $s; } + /** + * @return string + */ public function __toString() { return $this->html(); } } - diff --git a/src/mixins.php b/src/mixins.php index b0b8f03..3026b13 100644 --- a/src/mixins.php +++ b/src/mixins.php @@ -10,7 +10,7 @@ class Mixins { foreach ($mixins as $class) { if (!class_exists($class)) - throw new Exception('Tried to inherit non-existant class \''.$class.'\'.'); + throw new \Exception('Tried to inherit non-existant class \''.$class.'\'.'); $mixin = new $class($object); $methods = get_class_methods($mixin); @@ -27,7 +27,7 @@ class Mixins { $m = strtolower($method); if (!isset($this->methods[$m])) - throw new Exception('Call to undefined method ' . get_class($this) . "::$method()"); + throw new \Exception('Call to undefined method ' . get_class($this) . "::$method()"); $this->current_call_method = $method; $this->current_call_mixins = $this->methods[$m]; @@ -40,11 +40,11 @@ class Mixins public function call_next_method() { if ($this->current_call_mixins === null) - throw new Exception('call_next_method is invoked outside of mixin method.'); + throw new \Exception('call_next_method is invoked outside of mixin method.'); $mixin = array_pop($this->current_call_mixins); if ($mixin === null) - throw new Exception('No next method for ' . get_class($this) . "::$method()"); + throw new \Exception('No next method for ' . get_class($this) . "::$method()"); return call_user_func_array(array($mixin, $this->current_call_method), func_get_args()); } diff --git a/src/shell.php b/src/shell.php index 03e8468..6aa5f0d 100644 --- a/src/shell.php +++ b/src/shell.php @@ -6,10 +6,10 @@ class Shell { public static function call() { - return self::callv(func_get_args()); + self::callv(func_get_args()); } - public static function callv($command) + public static function callv(array $command) { $str = ''; foreach ($command as $part) { @@ -24,4 +24,3 @@ class Shell proc_close($process); } } - -- 2.49.0