Skip to content

Commit

Permalink
Apply fixes from StyleCI (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Nov 30, 2019
1 parent fc18775 commit ba3128d
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 99 deletions.
9 changes: 5 additions & 4 deletions src/PhpOption/LazyOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ final class LazyOption extends Option
* Helper Constructor.
*
* @param callable $callback
* @param array $arguments
* @param array $arguments
*
* @return LazyOption
*/
public static function create($callback, array $arguments = array())
public static function create($callback, array $arguments = [])
{
return new self($callback, $arguments);
}
Expand All @@ -46,9 +46,9 @@ public static function create($callback, array $arguments = array())
* Constructor.
*
* @param callable $callback
* @param array $arguments
* @param array $arguments
*/
public function __construct($callback, array $arguments = array())
public function __construct($callback, array $arguments = [])
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Invalid callback given');
Expand Down Expand Up @@ -160,6 +160,7 @@ private function option()
$this->option = call_user_func_array($this->callback, $this->arguments);
if (!$this->option instanceof Option) {
$this->option = null;

throw new \RuntimeException('Expected instance of \PhpOption\Option');
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/PhpOption/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ public function foldRight($initialValue, $callable)
return $initialValue;
}

private function __construct() { }
private function __construct()
{
}
}
40 changes: 20 additions & 20 deletions src/PhpOption/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class Option implements IteratorAggregate
* convert them to an option. By default, we treat ``null`` as the None case,
* and everything else as Some.
*
* @param mixed $value The actual return value.
* @param mixed $value The actual return value.
* @param mixed $noneValue The value which should be considered "None"; null
* by default.
*
Expand All @@ -57,8 +57,8 @@ public static function fromValue($value, $noneValue = null)
*
* Otherwise, Some is returned wrapping the value at the given key.
*
* @param mixed $array a potential array value
* @param string $key the key to check
* @param mixed $array a potential array value
* @param string $key the key to check
*
* @return Option
*/
Expand All @@ -78,16 +78,16 @@ public static function fromArraysValue($array, $key)
* the return value is not yet an option. By default, we treat ``null`` as
* None case, and everything else as Some.
*
* @param callable $callback The callback to evaluate.
* @param array $arguments
* @param mixed $noneValue The value which should be considered "None"; null
* by default.
* @param callable $callback The callback to evaluate.
* @param array $arguments
* @param mixed $noneValue The value which should be considered "None"; null
* by default.
*
* @return Option
*/
public static function fromReturn($callback, array $arguments = array(), $noneValue = null)
public static function fromReturn($callback, array $arguments = [], $noneValue = null)
{
return new LazyOption(function() use ($callback, $arguments, $noneValue) {
return new LazyOption(function () use ($callback, $arguments, $noneValue) {
$return = call_user_func_array($callback, $arguments);

if ($return === $noneValue) {
Expand All @@ -107,26 +107,26 @@ public static function fromReturn($callback, array $arguments = array(), $noneVa
* to Option::fromValue() method.
*
* @param Option|callable|mixed $value
* @param mixed $noneValue used when $value is mixed or callable, for None-check
* @param mixed $noneValue used when $value is mixed or callable, for None-check
*
* @return Option
*/
public static function ensure($value, $noneValue = null)
{
if ($value instanceof Option) {
if ($value instanceof self) {
return $value;
} elseif (is_callable($value)) {
return new LazyOption(function() use ($value, $noneValue) {
return new LazyOption(function () use ($value, $noneValue) {
$return = $value();

if ($return instanceof Option) {
if ($return instanceof self) {
return $return;
} else {
return Option::fromValue($return, $noneValue);
return self::fromValue($return, $noneValue);
}
});
} else {
return Option::fromValue($value, $noneValue);
return self::fromValue($value, $noneValue);
}
}

Expand Down Expand Up @@ -199,7 +199,7 @@ abstract public function isDefined();
*
* @return Option
*/
abstract public function orElse(Option $else);
abstract public function orElse(self $else);

/**
* This is similar to map() below except that the return value has no meaning;
Expand Down Expand Up @@ -342,8 +342,8 @@ abstract public function reject($value);
* }
* ```
*
* @param mixed $initialValue
* @param callable $callable function(initialValue, callable): result
* @param mixed $initialValue
* @param callable $callable function(initialValue, callable): result
*
* @return mixed
*/
Expand All @@ -352,8 +352,8 @@ abstract public function foldLeft($initialValue, $callable);
/**
* foldLeft() but with reversed arguments for the callable.
*
* @param mixed $initialValue
* @param callable $callable function(callable, initialValue): result
* @param mixed $initialValue
* @param callable $callable function(callable, initialValue): result
*
* @return mixed
*/
Expand Down
4 changes: 2 additions & 2 deletions src/PhpOption/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function map($callable)
public function flatMap($callable)
{
$rs = call_user_func($callable, $this->value);
if ( ! $rs instanceof Option) {
if (!$rs instanceof Option) {
throw new \RuntimeException('Callables passed to flatMap() must return an Option. Maybe you should use map() instead?');
}

Expand Down Expand Up @@ -137,7 +137,7 @@ public function reject($value)

public function getIterator()
{
return new ArrayIterator(array($this->value));
return new ArrayIterator([$this->value]);
}

public function foldLeft($initialValue, $callable)
Expand Down
29 changes: 21 additions & 8 deletions tests/PhpOption/Tests/EnsureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpOption\Some;

/**
* Tests for Option::ensure() method
* Tests for Option::ensure() method.
*
* @covers \PhpOption\Option::ensure
*/
Expand All @@ -17,6 +17,7 @@ protected function ensure($value, $noneValue = null)
{
$option = Option::ensure($value, $noneValue);
$this->assertInstanceOf('PhpOption\Option', $option);

return $option;
}

Expand All @@ -26,16 +27,21 @@ public function testMixedValue()
$this->assertTrue($option->isDefined());
$this->assertSame(1, $option->get());
$this->assertFalse($this->ensure(null)->isDefined());
$this->assertFalse($this->ensure(1,1)->isDefined());
$this->assertFalse($this->ensure(1, 1)->isDefined());
}

public function testReturnValue()
{
$option = $this->ensure(function() { return 1; });
$option = $this->ensure(function () {
return 1;
});
$this->assertTrue($option->isDefined());
$this->assertSame(1, $option->get());
$this->assertFalse($this->ensure(function() { return null; })->isDefined());
$this->assertFalse($this->ensure(function() { return 1; }, 1)->isDefined());
$this->assertFalse($this->ensure(function () {
})->isDefined());
$this->assertFalse($this->ensure(function () {
return 1;
}, 1)->isDefined());
}

public function testOptionReturnsAsSameInstance()
Expand All @@ -46,17 +52,24 @@ public function testOptionReturnsAsSameInstance()

public function testOptionReturnedFromClosure()
{
$option = $this->ensure(function() { return Some::create(1); });
$option = $this->ensure(function () {
return Some::create(1);
});
$this->assertTrue($option->isDefined());
$this->assertSame(1, $option->get());

$option = $this->ensure(function() { return None::create(); });
$option = $this->ensure(function () {
return None::create();
});
$this->assertFalse($option->isDefined());
}

public function testClosureReturnedFromClosure()
{
$option = $this->ensure(function() { return function() {}; });
$option = $this->ensure(function () {
return function () {
};
});
$this->assertTrue($option->isDefined());
$this->assertInstanceOf('Closure', $option->get());
}
Expand Down
37 changes: 23 additions & 14 deletions tests/PhpOption/Tests/LazyOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public function setUp()
{
$this->subject = $this
->getMockBuilder('Subject')
->setMethods(array('execute'))
->setMethods(['execute'])
->getMock();
}

public function testGetWithArgumentsAndConstructor()
{
$some = \PhpOption\LazyOption::create(array($this->subject, 'execute'), array('foo'));
$some = \PhpOption\LazyOption::create([$this->subject, 'execute'], ['foo']);

$this->subject
->expects($this->once())
Expand All @@ -35,7 +35,7 @@ public function testGetWithArgumentsAndConstructor()

public function testGetWithArgumentsAndCreate()
{
$some = new \PhpOption\LazyOption(array($this->subject, 'execute'), array('foo'));
$some = new \PhpOption\LazyOption([$this->subject, 'execute'], ['foo']);

$this->subject
->expects($this->once())
Expand All @@ -52,7 +52,7 @@ public function testGetWithArgumentsAndCreate()

public function testGetWithoutArgumentsAndConstructor()
{
$some = new \PhpOption\LazyOption(array($this->subject, 'execute'));
$some = new \PhpOption\LazyOption([$this->subject, 'execute']);

$this->subject
->expects($this->once())
Expand All @@ -68,7 +68,7 @@ public function testGetWithoutArgumentsAndConstructor()

public function testGetWithoutArgumentsAndCreate()
{
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
Expand All @@ -89,7 +89,7 @@ public function testGetWithoutArgumentsAndCreate()
*/
public function testCallbackReturnsNull()
{
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
Expand All @@ -99,7 +99,9 @@ public function testCallbackReturnsNull()
$this->assertFalse($option->isDefined());
$this->assertTrue($option->isEmpty());
$this->assertEquals('alt', $option->getOrElse('alt'));
$this->assertEquals('alt', $option->getOrCall(function(){return 'alt';}));
$this->assertEquals('alt', $option->getOrCall(function () {
return 'alt';
}));

$option->get();
}
Expand All @@ -110,7 +112,7 @@ public function testCallbackReturnsNull()
*/
public function testExceptionIsThrownIfCallbackReturnsNonOption()
{
$option = \PhpOption\LazyOption::create(array($this->subject, 'execute'));
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
Expand Down Expand Up @@ -142,7 +144,7 @@ public function testifDefined()
{
$called = false;
$self = $this;
$this->assertNull(LazyOption::fromValue('foo')->ifDefined(function($v) use (&$called, $self) {
$this->assertNull(LazyOption::fromValue('foo')->ifDefined(function ($v) use (&$called, $self) {
$called = true;
$self->assertEquals('foo', $v);
}));
Expand All @@ -153,7 +155,7 @@ public function testForAll()
{
$called = false;
$self = $this;
$this->assertInstanceOf('PhpOption\Some', LazyOption::fromValue('foo')->forAll(function($v) use (&$called, $self) {
$this->assertInstanceOf('PhpOption\Some', LazyOption::fromValue('foo')->forAll(function ($v) use (&$called, $self) {
$called = true;
$self->assertEquals('foo', $v);
}));
Expand All @@ -163,28 +165,35 @@ public function testForAll()
public function testOrElse()
{
$some = \PhpOption\Some::create('foo');
$lazy = \PhpOption\LazyOption::create(function() use ($some) {return $some;});
$lazy = \PhpOption\LazyOption::create(function () use ($some) {
return $some;
});
$this->assertSame($some, $lazy->orElse(\PhpOption\None::create()));
$this->assertSame($some, $lazy->orElse(\PhpOption\Some::create('bar')));
}

public function testFoldLeftRight()
{
$callback = function() { };
$callback = function () {
};

$option = $this->getMockForAbstractClass('PhpOption\Option');
$option->expects($this->once())
->method('foldLeft')
->with(5, $callback)
->will($this->returnValue(6));
$lazyOption = new LazyOption(function() use ($option) { return $option; });
$lazyOption = new LazyOption(function () use ($option) {
return $option;
});
$this->assertSame(6, $lazyOption->foldLeft(5, $callback));

$option->expects($this->once())
->method('foldRight')
->with(5, $callback)
->will($this->returnValue(6));
$lazyOption = new LazyOption(function() use ($option) { return $option; });
$lazyOption = new LazyOption(function () use ($option) {
return $option;
});
$this->assertSame(6, $lazyOption->foldRight(5, $callback));
}
}
Loading

0 comments on commit ba3128d

Please sign in to comment.