Better type description on PHPDoc, better testing of returned value.

This commit is contained in:
SpacePossum
2016-12-08 11:42:53 +01:00
parent e4125e6ef9
commit 282df533de
5 changed files with 97 additions and 4 deletions
+1 -1
View File
@@ -596,7 +596,7 @@ An extension is a class that implements the following interface::
/**
* Returns a list of operators to add to the existing list.
*
* @return array An array of operators
* @return array<array> First array of unary operators, second array of binary operators
*/
function getOperators();
+1 -1
View File
@@ -580,7 +580,7 @@ An extension is a class that implements the following interface::
/**
* Returns a list of operators to add to the existing list.
*
* @return array An array of operators
* @return array<array> First array of unary operators, second array of binary operators
*/
function getOperators();
+8 -1
View File
@@ -1512,8 +1512,15 @@ class Twig_Environment
// operators
if ($operators = $extension->getOperators()) {
if (!is_array($operators)) {
throw new InvalidArgumentException(sprintf(
'"%s::getOperators()" must return an array with operators, got "%s".',
get_class($extension), is_object($operators) ? get_class($operators) : gettype($operators).(is_resource($operators) ? '' : '#'.$operators)
));
}
if (2 !== count($operators)) {
throw new InvalidArgumentException(sprintf('"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
throw new InvalidArgumentException(sprintf('"%s::getOperators()" must return an array of 2 elements, got %d.', get_class($extension), count($operators)));
}
$this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
+1 -1
View File
@@ -63,7 +63,7 @@ interface Twig_ExtensionInterface
/**
* Returns a list of operators to add to the existing list.
*
* @return array An array of operators
* @return array<array> First array of unary operators, second array of binary operators
*/
public function getOperators();
+86
View File
@@ -0,0 +1,86 @@
<?php
final class CustomExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @param Twig_ExtensionInterface $extension
* @param string $expectedExceptionMessage
*
* @requires PHP 5.3
* @dataProvider provideInvalidExtensions
*/
public function testGetInvalidOperators(\Twig_ExtensionInterface $extension, $expectedExceptionMessage)
{
$this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
$loader = new \Twig_Loader_Array(array('foo' => '{{ foo }}'));
$env = new \Twig_Environment($loader);
$env->addExtension($extension);
$method = new \ReflectionMethod($env, 'initExtensions');
$method->setAccessible(true);
$method->invoke($env);
}
public function provideInvalidExtensions()
{
return array(
array(new InvalidOperatorExtension(new \stdClass()), '"InvalidOperatorExtension::getOperators()" must return an array with operators, got "stdClass".'),
array(new InvalidOperatorExtension(array(1, 2, 3)), '"InvalidOperatorExtension::getOperators()" must return an array of 2 elements, got 3.'),
);
}
}
final class InvalidOperatorExtension implements \Twig_ExtensionInterface
{
private $operators;
public function __construct($operators)
{
$this->operators = $operators;
}
public function initRuntime(Twig_Environment $environment)
{
}
public function getTokenParsers()
{
return array();
}
public function getNodeVisitors()
{
return array();
}
public function getFilters()
{
return array();
}
public function getTests()
{
return array();
}
public function getFunctions()
{
return array();
}
public function getGlobals()
{
return array();
}
public function getOperators()
{
return $this->operators;
}
public function getName()
{
return __CLASS__;
}
}