Skip to content

Commit

Permalink
updated for php 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoubik committed May 14, 2020
1 parent c69034b commit dcd3e43
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 66 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
level: 1
level: max
paths:
- src/
- tests/
7 changes: 0 additions & 7 deletions src/Sloth/InvalidArgumentException.php

This file was deleted.

66 changes: 44 additions & 22 deletions src/Sloth/LazyAccessor.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
<?php

declare(strict_types=1);

namespace Sloth;

class LazyAccessor
/**
* @template T
*/
final class LazyAccessor
{
/** @var callable */
/** @var callable(): object */
private $callback;

/** @var ?object */
private $object;

public function __construct($callback)
/**
* @param callable(): object $callback
*/
public function __construct(callable $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException();
}
$this->callback = $callback;
}

public function __get($name)
/**
* @return mixed
*/
public function __get(string $name)
{
$this->initialize();
return $this->object->$name;
return $this->getObject()->$name;
}

public function __set($name, $value)
/**
* @param mixed $value
*/
public function __set(string $name, $value): void
{
$this->initialize();
return $this->object->$name = $value;
$this->getObject()->$name = $value;
}

public function __isset($name)
public function __isset(string $name): bool
{
$this->initialize();
return isset($this->object->$name);
return isset($this->getObject()->$name);
}

public function __unset($name)
public function __unset(string $name): void
{
$this->initialize();
unset($this->object->$name);
unset($this->getObject()->$name);
}

public function __call($name, array $arguments)
/**
* @param mixed[] $arguments
* @return mixed
*/
public function __call(string $name, array $arguments)
{
$this->initialize();
return call_user_func_array(array($this->object, $name), $arguments);
$callable = [$this->getObject(), $name];

if (!is_callable($callable)) {
throw new \TypeError(sprintf('class \'%s\' does not have method \'%s\'', static::class, $name));
}

return call_user_func_array($callable, $arguments);
}

private function initialize()
/**
* @return object
*/
private function getObject()
{
if ($this->object === null) {
$this->object = call_user_func($this->callback);
}

return $this->object;
}
}
32 changes: 19 additions & 13 deletions src/Sloth/LazyIterator.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
<?php

declare(strict_types=1);

namespace Sloth;

class LazyIterator implements \IteratorAggregate, \Countable
/**
* @template TKey
* @template TValue
* @implements \IteratorAggregate<TKey, TValue>
*/
final class LazyIterator implements \IteratorAggregate, \Countable
{
/** @var callable */
/** @var callable(): iterable<TKey, TValue> */
private $callback;

/** @var \Iterator */
/** @var ?iterable<TKey, TValue> */
private $iterator;

/**
* @param $callback that returns \Traversable, \Iterator or array
* @param callable(): iterable<TKey, TValue> $callback
*/
public function __construct($callback)
public function __construct(callable $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException();
}
$this->callback = $callback;
}

/**
* @return \Traversable iterator
* @return iterable<TKey, TValue>
*/
public function getIterator()
public function getIterator(): iterable
{
if ($this->iterator !== null) {
return $this->iterator;
}

$iterator = call_user_func($this->callback);

if (is_array($iterator)) {
$this->iterator = new \ArrayIterator($iterator);
return $this->iterator;
}
$this->iterator = $iterator;
return $this->iterator;

return $this->iterator = $iterator;
}

public function count()
public function count(): int
{
return iterator_count($this);
}
Expand Down
19 changes: 10 additions & 9 deletions src/Sloth/LazyString.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
<?php

declare(strict_types=1);

namespace Sloth;

class LazyString
final class LazyString
{
/** @var callable */
/** @var callable(): string */
private $callback;

/** @var string */
/** @var ?string */
private $string;

/**
* @param $callback that returns \Traversable, \Iterator or array
* @param callable(): string $callback
*/
public function __construct($callback)
public function __construct(callable $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException();
}
$this->callback = $callback;
}

public function __toString()
public function __toString(): string
{
if ($this->string !== null) {
return $this->string;
}

$this->string = call_user_func($this->callback);

return $this->string;
}
}
4 changes: 2 additions & 2 deletions tests/LazyAccessor.checksCallable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require __DIR__ . '/bootstrap.php';

Assert::exception(function () {
Assert::throws(function () {
new Sloth\LazyAccessor('not callable');
}, 'Sloth\InvalidArgumentException');
}, TypeError::class);
4 changes: 4 additions & 0 deletions tests/LazyAccessor.methodCall.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ $greeter = new \Sloth\LazyAccessor($callback);
\Assert::equal('Hello world!', $greeter->sayHello('world'));
\Assert::equal('Hello John!', $greeter->sayHello('John'));

\Assert::throws(function () use ($greeter) {
$greeter->nonExistingMethod();
}, TypeError::class);

\Assert::equal(1, $countCalled);
6 changes: 3 additions & 3 deletions tests/LazyIterator.arrayIterator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require __DIR__ . '/bootstrap.php';

$callback = function () {
return new ArrayIterator(array(1, 2, 3));
return new ArrayIterator([1, 2, 3]);
};
$iterator = new Sloth\LazyIterator($callback);

$values = array();
$values = [];
foreach ($iterator as $value) {
$values[] = $value;
}
Expand All @@ -19,7 +19,7 @@ Assert::equal(3, $values[2]);


$callback = function () {
return new ArrayIterator(array());
return new ArrayIterator([]);
};
$iterator = new Sloth\LazyIterator($callback);
$values = iterator_to_array($iterator);
Expand Down
2 changes: 1 addition & 1 deletion tests/LazyIterator.callbackIsCalledOnlyOnce.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require __DIR__ . '/bootstrap.php';
$countCalled = 0;
$callback = function () use (&$countCalled) {
$countCalled++;
return new ArrayIterator(array());
return new ArrayIterator([]);
};
$iterator = new Sloth\LazyIterator($callback);

Expand Down
4 changes: 2 additions & 2 deletions tests/LazyIterator.checksCallable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require __DIR__ . '/bootstrap.php';

Assert::exception(function () {
Assert::throws(function () {
new Sloth\LazyIterator('not callable');
}, 'Sloth\InvalidArgumentException');
}, TypeError::class);
2 changes: 1 addition & 1 deletion tests/LazyIterator.implementsCountable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require __DIR__ . '/bootstrap.php';

$iterator = new Sloth\LazyIterator(function () {
return new ArrayIterator(array('a', 'b', 'c'));
return new ArrayIterator(['a', 'b', 'c']);
});

Assert::equal(3, count($iterator));
2 changes: 1 addition & 1 deletion tests/LazyIterator.retypesArrayToIterator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require __DIR__ . '/bootstrap.php';

$iterator = new Sloth\LazyIterator(function () {
return array(1 => 'a', 3 => 'b');
return [1 => 'a', 3 => 'b'];
});

Assert::true($iterator->getIterator() instanceof Iterator);
Expand Down
4 changes: 2 additions & 2 deletions tests/LazyString.checksCallable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require __DIR__ . '/bootstrap.php';

Assert::exception(function () {
Assert::throws(function () {
new Sloth\LazyString('not callable');
}, 'Sloth\InvalidArgumentException');
}, TypeError::class);
4 changes: 3 additions & 1 deletion tests/Mocks/Greeter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace SlothTests\Mocks;

class Greeter
{
public function sayHello($name)
public function sayHello(string $name): string
{
return "Hello $name!";
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Mocks/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

class Person
{
/** @var string */
public $name;

public function __construct($name)
public function __construct(string $name)
{
$this->name = $name;
}
Expand Down

0 comments on commit dcd3e43

Please sign in to comment.