Skip to content

Commit

Permalink
Better check in trait if the property is initialised and has correct …
Browse files Browse the repository at this point in the history
…type

Signed-off-by: Michał Bundyra <contact@webimpress.com>
  • Loading branch information
michalbundyra committed May 5, 2020
1 parent 1e29c68 commit c2a6e79
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Input/InputParamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use ArrayObject;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -31,6 +32,7 @@ trait InputParamTrait
/**
* @param null|mixed $default
* @return $this
* @throws RuntimeException
*/
final public function addParam(
string $name,
Expand All @@ -54,6 +56,12 @@ final public function addParam(

if ($this->inputParams === null) {
$this->inputParams = new ArrayObject();
} elseif (! $this->inputParams instanceof ArrayObject) {
throw new RuntimeException(sprintf(
'Command %s uses $inputParams property. It is not allowed while using %s',
static::class,
InputParamTrait::class
));
}

$this->inputParams->offsetSet($name, new InputParam($name, $description, $type, $required, $default, $options));
Expand All @@ -67,7 +75,7 @@ final public function addParam(
*/
final public function getParam(string $name)
{
if ($this->inputParams === null || ! $this->inputParams->offsetExists($name)) {
if (! $this->inputParams instanceof ArrayObject || ! $this->inputParams->offsetExists($name)) {
throw new InvalidArgumentException(sprintf('Invalid parameter name: %s', $name));
}

Expand Down

0 comments on commit c2a6e79

Please sign in to comment.