Skip to content

Commit

Permalink
secure TypeReflection for incorrect arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphhh committed Dec 28, 2015
1 parent 33617ca commit bbff6c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/TypeReflection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace TRex\Reflection;

use \InvalidArgumentException;


/**
* TypeReflection reflect the types.
Expand Down Expand Up @@ -390,8 +392,15 @@ private function getTypeMappingList()
* @param string $standardizeType
* @return array
*/
private function getTypeMapping($standardizeType = '')
private function getTypeMapping($standardizeType)
{
if(!isset($this->getTypeMappingList()[$standardizeType])){
throw new InvalidArgumentException(sprintf(
'$standardizeType not valid. Should be on of the values: %s. "%s" given.',
json_encode($this->getStandardizedTypes()),
$standardizeType
));
}
return $this->getTypeMappingList()[$standardizeType];
}

Expand Down
11 changes: 11 additions & 0 deletions tests/TypeReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public function tesIsCallable()
}
}

public function testIsWithoutValidStandardizedType()
{
$typeReflection = new TypeReflection('string');

$this->setExpectedException(
'InvalidArgumentException',
'$standardizeType not valid. Should be on of the values: ["void","mixed","null","boolean","string","integer","float","number","scalar","array","object","resource","callable"]. "foo" given.'
);
$this->assertFalse($typeReflection->is('foo'));
}

/**
* @param mixed $var
* @param string $type
Expand Down

0 comments on commit bbff6c7

Please sign in to comment.