Scalar type hints, return types, and camelCased methods

This commit is contained in:
Timothy Warren 2016-10-20 20:08:11 -04:00
parent ffaa4a4cab
commit 5890f14c50
28 changed files with 148 additions and 119 deletions

View File

@ -30,7 +30,7 @@ trait ArrayWrapper {
* @param array $arr
* @return ArrayType
*/
public function arr(array $arr)
public function arr(array $arr): ArrayType
{
return new ArrayType($arr);
}

View File

@ -19,6 +19,7 @@ namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Exception\ConfigException;
use Aviat\Ion\Model\DB;
use PDO;
/**
* Driver for caching via a traditional SQL database
@ -64,7 +65,7 @@ class SQLDriver extends DB implements DriverInterface {
->where('key', $key)
->get();
$row = $query->fetch(\PDO::FETCH_ASSOC);
$row = $query->fetch(PDO::FETCH_ASSOC);
if (empty($row))
{

View File

@ -16,9 +16,8 @@
namespace Aviat\Ion;
use Aviat\Ion\Exception\ConfigException;
use Aviat\Ion\Type\ArrayType;
use InvalidArgumentException;
/**
@ -31,18 +30,18 @@ class Config implements ConfigInterface {
/**
* Config object
*
* @var \Aviat\Ion\Type\ArrayType
* @var ArrayType
*/
protected $map = [];
/**
* Constructor
*
* @param array $config_array
* @param array $configArray
*/
public function __construct(array $config_array = [])
public function __construct(array $configArray = [])
{
$this->map = $this->arr($config_array);
$this->map = $this->arr($configArray);
}
/**
@ -56,7 +55,7 @@ class Config implements ConfigInterface {
{
if (is_array($key))
{
return $this->map->get_deep_key($key);
return $this->map->getDeepKey($key);
}
return $this->map->get($key);
@ -72,7 +71,7 @@ class Config implements ConfigInterface {
{
if (is_array($key))
{
$this->map->set_deep_key($key, NULL);
$this->map->setDeepKey($key, NULL);
}
else
{
@ -87,13 +86,13 @@ class Config implements ConfigInterface {
* @param integer|string|array $key
* @param mixed $value
* @throws InvalidArgumentException
* @return Config
* @return ConfigInterface
*/
public function set($key, $value)
public function set($key, $value): ConfigInterface
{
if (is_array($key))
{
$this->map->set_deep_key($key, $value);
$this->map->setDeepKey($key, $value);
}
else if (is_scalar($key) && ! empty($key))
{

View File

@ -36,7 +36,7 @@ interface ConfigInterface {
* @throws \InvalidArgumentException
* @return ConfigInterface
*/
public function set($key, $value);
public function set($key, $value): ConfigInterface;
/**
* Remove a config value

View File

@ -16,12 +16,9 @@
namespace Aviat\Ion\Di;
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
use Psr\Log\LoggerInterface;
use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Di\Exception\NotFoundException;
/**
* Dependency container
*/
@ -106,13 +103,13 @@ class Container implements ContainerInterface {
{
if ( ! is_string($id))
{
throw new ContainerException("Id must be a string");
throw new ContainerException('Id must be a string');
}
if ($this->has($id))
{
// By default, call a factory with the Container
$args = (is_array($args)) ? $args : [$this];
$args = is_array($args) ? $args : [$this];
$obj = call_user_func_array($this->container[$id], $args);
// Check for container interface, and apply the container to the object
@ -130,7 +127,7 @@ class Container implements ContainerInterface {
* @param Callable $value - a factory callable for the item
* @return ContainerInterface
*/
public function set($id, Callable $value)
public function set(string $id, Callable $value): ContainerInterface
{
$this->container[$id] = $value;
return $this;
@ -144,7 +141,7 @@ class Container implements ContainerInterface {
* @throws NotFoundException - No entry was found for this identifier.
* @return ContainerInterface
*/
public function setInstance($id, $value)
public function setInstance(string $id, $value): ContainerInterface
{
if ( ! $this->has($id))
{
@ -162,7 +159,7 @@ class Container implements ContainerInterface {
* @param string $id Identifier of the entry to look for.
* @return boolean
*/
public function has($id)
public function has($id): bool
{
return array_key_exists($id, $this->container);
}
@ -173,7 +170,7 @@ class Container implements ContainerInterface {
* @param string $id The logger channel
* @return boolean
*/
public function hasLogger($id = 'default')
public function hasLogger(string $id = 'default'): bool
{
return array_key_exists($id, $this->loggers);
}
@ -185,7 +182,7 @@ class Container implements ContainerInterface {
* @param string $id The logger 'channel'
* @return ContainerInterface
*/
public function setLogger(LoggerInterface $logger, $id = 'default')
public function setLogger(LoggerInterface $logger, string $id = 'default'): ContainerInterface
{
$this->loggers[$id] = $logger;
return $this;
@ -197,9 +194,9 @@ class Container implements ContainerInterface {
* @param string $id The logger to retrieve
* @return LoggerInterface|null
*/
public function getLogger($id = 'default')
public function getLogger(string $id = 'default')
{
return ($this->hasLogger($id))
return $this->hasLogger($id)
? $this->loggers[$id]
: NULL;
}
@ -217,8 +214,8 @@ class Container implements ContainerInterface {
$trait_name = __NAMESPACE__ . '\\ContainerAware';
$interface_name = __NAMESPACE__ . '\\ContainerAwareInterface';
$uses_trait = in_array($trait_name, class_uses($obj));
$implements_interface = in_array($interface_name, class_implements($obj));
$uses_trait = in_array($trait_name, class_uses($obj), TRUE);
$implements_interface = in_array($interface_name, class_implements($obj), TRUE);
if ($uses_trait OR $implements_interface)
{

View File

@ -45,7 +45,7 @@ trait ContainerAware {
*
* @return ContainerInterface
*/
public function getContainer()
public function getContainer(): ContainerInterface
{
return $this->container;
}

View File

@ -34,7 +34,7 @@ interface ContainerAwareInterface {
*
* @return ContainerInterface
*/
public function getContainer();
public function getContainer(): ContainerInterface;
}
// End of ContainerAwareInterface.php

View File

@ -16,13 +16,36 @@
namespace Aviat\Ion\Di;
use Interop\Container\ContainerInterface as InteropInterface;
use Psr\Log\LoggerInterface;
/**
* Interface for the Dependency Injection Container
*
* Based on container-interop interface, but return types and
* scalar type hints make the interface incompatible to the PHP parser
*
* @see https://github.com/container-interop/container-interop
*/
interface ContainerInterface extends InteropInterface {
interface ContainerInterface {
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
* @throws NotFoundException No entry was found for this identifier.
* @throws ContainerException Error while retrieving the entry.
* @return mixed Entry.
*/
public function get($id);
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
* @return boolean
*/
public function has($id): bool;
/**
* Add a factory to the container
@ -31,7 +54,7 @@ interface ContainerInterface extends InteropInterface {
* @param Callable $value - a factory callable for the item
* @return ContainerInterface
*/
public function set($id, Callable $value);
public function set(string $id, Callable $value): ContainerInterface;
/**
* Set a specific instance in the container for an existing factory
@ -40,7 +63,7 @@ interface ContainerInterface extends InteropInterface {
* @param mixed $value
* @return ContainerInterface
*/
public function setInstance($id, $value);
public function setInstance(string $id, $value): ContainerInterface;
/**
* Get a new instance of the specified item
@ -55,22 +78,22 @@ interface ContainerInterface extends InteropInterface {
* @param string $id The logger channel
* @return boolean
*/
public function hasLogger($id = 'default');
public function hasLogger(string $id = 'default'): bool;
/**
* Add a logger to the Container
*
* @param LoggerInterface $logger
* @param string $id The logger 'channel'
* @return Container
* @return ContainerInterface
*/
public function setLogger(LoggerInterface $logger, $id = 'default');
public function setLogger(LoggerInterface $logger, string $id = 'default'): ContainerInterface;
/**
* Retrieve a logger for the selected channel
*
* @param string $id The logger to retreive
* @param string $id The logger to retrieve
* @return LoggerInterface|null
*/
public function getLogger($id = 'default');
public function getLogger(string $id = 'default');
}

View File

@ -16,10 +16,13 @@
namespace Aviat\Ion\Di\Exception;
use Exception;
use Interop\Container\Exception\ContainerException as InteropContainerException;
/**
* Generic exception for Di Container
*/
class ContainerException extends \Exception implements \Interop\Container\Exception\ContainerException {
class ContainerException extends Exception implements InteropContainerException {
}
// End of ContainerException.php

View File

@ -16,11 +16,13 @@
namespace Aviat\Ion\Di\Exception;
use Interop\Container\Exception\NotFoundException as InteropNotFoundException;
/**
* Exception for Di Container when trying to access a
* key that doesn't exist in the container
*/
class NotFoundException extends ContainerException implements \Interop\Container\Exception\NotFoundException {
class NotFoundException extends ContainerException implements InteropNotFoundException {
}
// End of NotFoundException.php

View File

@ -33,7 +33,7 @@ abstract class Enum {
*
* @return array
*/
protected function getConstList()
protected function getConstList(): array
{
$reflect = new ReflectionClass($this);
return $reflect->getConstants();
@ -44,7 +44,7 @@ abstract class Enum {
* @param mixed $key
* @return boolean
*/
protected function isValid($key)
protected function isValid($key): bool
{
$values = array_values($this->getConstList());
return in_array($key, $values);

View File

@ -16,9 +16,11 @@
namespace Aviat\Ion\Exception;
use InvalidArgumentException;
/**
* Exception for bad configuration
*/
class ConfigException extends \InvalidArgumentException {
class ConfigException extends InvalidArgumentException {
}

View File

@ -16,10 +16,13 @@
namespace Aviat\Ion\Exception;
use Exception;
use LogicException;
/**
* Exception called when a view is attempted to be sent twice
*/
class DoubleRenderException extends \LogicException {
class DoubleRenderException extends LogicException {
/**
* DoubleRenderException constructor.
@ -28,7 +31,7 @@ class DoubleRenderException extends \LogicException {
* @param int $code
* @param null $previous
*/
public function __construct($message = 'A view can only be rendered once, because headers can only be sent once.', $code = 0, $previous = NULL)
public function __construct(string $message = 'A view can only be rendered once, because headers can only be sent once.', int $code = 0, Exception $previous = NULL)
{
parent::__construct($message, $code, $previous);
}

View File

@ -16,11 +16,11 @@
namespace Aviat\Ion;
use BadMethodCallException;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use InvalidArgumentException;
use BadMethodCallException;
/**
* Friend class for testing
@ -62,7 +62,7 @@ class Friend {
* @param string $key
* @return mixed
*/
public function __get($key)
public function __get(string $key)
{
if ($this->_reflect_->hasProperty($key))
{
@ -80,7 +80,7 @@ class Friend {
* @param mixed $value
* @return void
*/
public function __set($key, $value)
public function __set(string $key, $value)
{
if ($this->_reflect_->hasProperty($key))
{
@ -97,7 +97,7 @@ class Friend {
* @return mixed
* @throws BadMethodCallException
*/
public function __call($method, $args)
public function __call(string $method, array $args)
{
if ( ! $this->_reflect_->hasMethod($method))
{
@ -116,7 +116,7 @@ class Friend {
* @param string $name
* @return ReflectionProperty|null
*/
private function _get_property($name)
private function _get_property(string $name)
{
try
{

View File

@ -43,26 +43,26 @@ class Json {
*
* @param string $filename
* @param mixed $data
* @param int $json_options - Options to pass to json_encode
* @param int $file_options - Options to pass to file_get_contents
* @param int $jsonOptions - Options to pass to json_encode
* @param int $fileOptions - Options to pass to file_get_contents
* @return int
*/
public static function encodeFile($filename, $data, $json_options = 0, $file_options = 0)
public static function encodeFile(string $filename, $data, int $jsonOptions = 0, int $fileOptions = 0): int
{
$json = self::encode($data, $json_options);
return file_put_contents($filename, $json, $file_options);
$json = self::encode($data, $jsonOptions);
return file_put_contents($filename, $json, $fileOptions);
}
/**
* Decode data from json
*
* @param string $json
* @param string|null $json
* @param bool $assoc
* @param int $depth
* @param int $options
* @return mixed
*/
public static function decode($json, $assoc = TRUE, $depth = 512, $options = 0)
public static function decode($json, bool $assoc = TRUE, int $depth = 512, int $options = 0)
{
// Don't try to decode null
if (empty($json))
@ -86,7 +86,7 @@ class Json {
* @param int $options
* @return mixed
*/
public static function decodeFile($filename, $assoc = TRUE, $depth = 512, $options = 0)
public static function decodeFile(string $filename, bool $assoc = TRUE, int $depth = 512, int $options = 0)
{
$json = file_get_contents($filename);
return self::decode($json, $assoc, $depth, $options);
@ -98,7 +98,7 @@ class Json {
* @param string $string
* @return boolean
*/
public static function isJson($string)
public static function isJson(string $string): bool
{
return StringType::create($string)->isJson();
}

View File

@ -16,10 +16,12 @@
namespace Aviat\Ion;
use InvalidArgumentException;
/**
* Exceptions thrown by the Json class
*/
class JsonException extends \InvalidArgumentException {
class JsonException extends InvalidArgumentException {
}
// End of JsonException.php

View File

@ -37,7 +37,7 @@ trait StaticInstance {
* @param array $args
* @return mixed
*/
public function __call($method, $args)
public function __call(string $method, array $args)
{
if (method_exists($this, $method))
{
@ -53,7 +53,7 @@ trait StaticInstance {
* @param array $args
* @return mixed
*/
public static function __callStatic($method, $args)
public static function __callStatic(string $method, array $args)
{
$class = get_called_class();
if ( ! array_key_exists($class, self::$instance))

View File

@ -16,12 +16,14 @@
namespace Aviat\Ion\Transformer;
use Aviat\Ion\StringWrapper;
/**
* Base class for data trasformation
*/
abstract class AbstractTransformer implements TransformerInterface {
use \Aviat\Ion\StringWrapper;
use StringWrapper;
/**
* Mutate the data structure
@ -37,7 +39,7 @@ abstract class AbstractTransformer implements TransformerInterface {
* @param array|object $collection
* @return array
*/
public function transform_collection($collection)
public function transformCollection($collection): array
{
$list = (array)$collection;
return array_map([$this, 'transform'], $list);

View File

@ -16,6 +16,8 @@
namespace Aviat\Ion\Type;
use InvalidArgumentException;
/**
* Wrapper class for native array methods for convenience
*
@ -37,7 +39,7 @@ class ArrayType {
*
* @var array
*/
protected $native_methods = [
protected $nativeMethods = [
'chunk' => 'array_chunk',
'pluck' => 'array_column',
'key_diff' => 'array_diff_key',
@ -62,7 +64,7 @@ class ArrayType {
*
* @var array
*/
protected $native_in_place_methods = [
protected $nativeInPlaceMethods = [
'shuffle' => 'shuffle',
'shift' => 'array_shift',
'unshift' => 'array_unshift',
@ -88,35 +90,35 @@ class ArrayType {
* @return mixed
* @throws \InvalidArgumentException
*/
public function __call($method, $args)
public function __call(string $method, array $args)
{
// Simple mapping for the majority of methods
if (array_key_exists($method, $this->native_methods))
if (array_key_exists($method, $this->nativeMethods))
{
$func = $this->native_methods[$method];
$func = $this->nativeMethods[$method];
// Set the current array as the first argument of the method
array_unshift($args, $this->arr);
return call_user_func_array($func, $args);
}
// Mapping for in-place methods
if (array_key_exists($method, $this->native_in_place_methods))
if (array_key_exists($method, $this->nativeInPlaceMethods))
{
$func = $this->native_in_place_methods[$method];
$func = $this->nativeInPlaceMethods[$method];
$func($this->arr);
return $this->arr;
}
throw new \InvalidArgumentException("Method '{$method}' does not exist");
throw new InvalidArgumentException("Method '{$method}' does not exist");
}
/**
* Does the passed key exist in the current array?
*
* @param string $key
* @param int|string $key
* @return bool
*/
public function has_key($key)
public function hasKey($key): bool
{
return array_key_exists($key, $this->arr);
}
@ -129,7 +131,7 @@ class ArrayType {
* @param mixed $value
* @return array
*/
public function fill($start_index, $num, $value)
public function fill(int $start_index, int $num, $value): array
{
return array_fill($start_index, $num, $value);
}
@ -140,7 +142,7 @@ class ArrayType {
* @param callable $callback
* @return array
*/
public function map(callable $callback)
public function map(callable $callback): array
{
return array_map($callback, $this->arr);
}
@ -152,7 +154,7 @@ class ArrayType {
* @param bool $strict
* @return false|integer|string
*/
public function search($value, $strict = FALSE)
public function search($value, bool $strict = TRUE)
{
return array_search($value, $this->arr, $strict);
}
@ -164,7 +166,7 @@ class ArrayType {
* @param bool $strict
* @return bool
*/
public function has($value, $strict = FALSE)
public function has($value, bool $strict = TRUE): bool
{
return in_array($value, $this->arr, $strict);
}
@ -184,7 +186,7 @@ class ArrayType {
}
else
{
if ($this->has_key($key))
if ($this->hasKey($key))
{
$value =& $this->arr[$key];
}
@ -200,7 +202,7 @@ class ArrayType {
* @param mixed $value
* @return ArrayType
*/
public function set($key, $value)
public function set($key, $value): ArrayType
{
$this->arr[$key] = $value;
return $this;
@ -212,7 +214,7 @@ class ArrayType {
* @param array $key
* @return mixed
*/
public function &get_deep_key(array $key)
public function &getDeepKey(array $key)
{
$pos =& $this->arr;
@ -241,7 +243,7 @@ class ArrayType {
* @param mixed $value
* @return array
*/
public function set_deep_key(array $key, $value)
public function setDeepKey(array $key, $value): array
{
$pos =& $this->arr;

View File

@ -75,7 +75,7 @@ abstract class View
* @throws DoubleRenderException
* @return string
*/
public function __toString()
public function __toString(): string
{
if ($this->hasRendered)
{
@ -88,10 +88,10 @@ abstract class View
/**
* Set the output string
*
* @param string $string
* @param mixed $string
* @return ViewInterface
*/
public function setOutput($string)
public function setOutput($string): ViewInterface
{
$this->response->getBody()->write($string);
@ -104,7 +104,7 @@ abstract class View
* @param string $string
* @return ViewInterface
*/
public function appendOutput($string)
public function appendOutput(string $string): ViewInterface
{
return $this->setOutput($string);
}
@ -115,7 +115,7 @@ abstract class View
*
* @return string
*/
public function getOutput()
public function getOutput(): string
{
return $this->response->getBody()->__toString();
}

View File

@ -55,7 +55,7 @@ class HtmlView extends HttpView {
* @param array $data
* @return string
*/
public function render_template($path, $data)
public function renderTemplate(string $path, array $data): string
{
$data['helper'] = $this->helper;
$data['escape'] = $this->helper->escape();

View File

@ -41,7 +41,7 @@ class HttpView extends BaseView {
* @param int $code
* @return void
*/
public function redirect($url, $code)
public function redirect(string $url, int $code)
{
ob_start();
$message = $this->response->getReasonPhrase($code);
@ -64,7 +64,7 @@ class HttpView extends BaseView {
* @param int $code
* @return HttpView
*/
public function setStatusCode($code)
public function setStatusCode(int $code): HttpView
{
$this->response = $this->response->withStatus($code)
->withProtocolVersion('1.1');

View File

@ -37,7 +37,7 @@ class JsonView extends HttpView {
* @param mixed $string
* @return ViewInterface
*/
public function setOutput($string)
public function setOutput($string): ViewInterface
{
if ( ! is_string($string))
{

View File

@ -29,15 +29,15 @@ interface ViewInterface {
* @throws DoubleRenderException
* @return string
*/
public function __toString();
public function __toString(): string;
/**
* Set the output string
*
* @param string $string
* @param mixed $string
* @return ViewInterface
*/
public function setOutput($string);
public function setOutput($string): ViewInterface;
/**
* Append additional output.
@ -45,7 +45,7 @@ interface ViewInterface {
* @param string $string
* @return ViewInterface
*/
public function appendOutput($string);
public function appendOutput(string $string): ViewInterface;
/**
* Get the current output as a string. Does not
@ -53,7 +53,7 @@ interface ViewInterface {
*
* @return string
*/
public function getOutput();
public function getOutput(): string;
/**
* Send output to client. As it renders the view,

View File

@ -16,7 +16,7 @@
namespace Aviat\Ion\Tests\Di;
use Aviat\Ion\Di\Container;
use Aviat\Ion\Di\{Container, ContainerAware};
use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Tests\Ion_TestCase;
use Monolog\Logger;
@ -32,12 +32,11 @@ class FooTest {
}
class FooTest2 {
use \Aviat\Ion\Di\ContainerAware;
use ContainerAware;
}
class ContainerTest extends Ion_TestCase {
public function setUp()
{
$this->container = new Container();
@ -85,15 +84,9 @@ class ContainerTest extends Ion_TestCase {
*/
public function testGetNewWithException($id, $exception, $message)
{
try
{
$this->container->getNew($id);
}
catch(ContainerException $e)
{
$this->assertInstanceOf($exception, $e);
$this->assertEquals($message, $e->getMessage());
}
$this->expectException($exception);
$this->expectExceptionMessage($message);
$this->container->getNew($id);
}
public function dataSetInstanceWithException()

View File

@ -102,7 +102,7 @@ class AbstractTransformerTest extends Ion_TestCase {
*/
public function testTransformCollection($original, $expected)
{
$actual = $this->transformer->transform_collection($original);
$actual = $this->transformer->transformCollection($original);
$this->assertEquals($expected, $actual);
}
}

View File

@ -124,8 +124,8 @@ class ArrayTypeTest extends Ion_TestCase {
]
];
$obj = $this->arr($arr);
$this->assertEquals('foobar', $obj->get_deep_key(['baz', 'bar']));
$this->assertNull($obj->get_deep_key(['foo', 'bar', 'baz']));
$this->assertEquals('foobar', $obj->getDeepKey(['baz', 'bar']));
$this->assertNull($obj->getDeepKey(['foo', 'bar', 'baz']));
}
public function testMap()
@ -165,8 +165,8 @@ class ArrayTypeTest extends Ion_TestCase {
'a' => 'b',
'z' => 'y'
]);
$this->assertTrue($obj->has_key('a'));
$this->assertFalse($obj->has_key('b'));
$this->assertTrue($obj->hasKey('a'));
$this->assertFalse($obj->hasKey('b'));
}
public function testHas()

View File

@ -34,7 +34,7 @@ class HtmlViewTest extends HttpViewTest {
{
$path = _dir(self::TEST_VIEW_DIR, 'test_view.php');
$expected = '<tag>foo</tag>';
$actual = $this->view->render_template($path, [
$actual = $this->view->renderTemplate($path, [
'var' => 'foo'
]);
$this->assertEquals($expected, $actual);