Skip to content

Commit

Permalink
Merge pull request #46 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Oct 24, 2022
2 parents 758b96c + 86ba2a3 commit 83c3ff8
Show file tree
Hide file tree
Showing 27 changed files with 180 additions and 154 deletions.
12 changes: 2 additions & 10 deletions src/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@

namespace Laminas\Paginator\Adapter;

use ReturnTypeWillChange; // phpcs:ignore
use ReturnTypeWillChange;

use function array_slice;
use function count;

class ArrayAdapter implements AdapterInterface
{
/**
* ArrayAdapter
*
* @var array
*/
protected $array;

/**
* Item count
*
Expand All @@ -28,9 +21,8 @@ class ArrayAdapter implements AdapterInterface
/**
* @param array $array ArrayAdapter to paginate
*/
public function __construct(array $array = [])
public function __construct(protected array $array = [])
{
$this->array = $array;
$this->count = count($array);
}

Expand Down
24 changes: 5 additions & 19 deletions src/Adapter/DbSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ class DbSelect implements AdapterInterface
/** @var Sql */
protected $sql;

/**
* Database query
*
* @var Select
*/
protected $select;

/**
* Database count query
*
* @var Select|null
*/
protected $countSelect;

/** @var ResultSet */
protected $resultSetPrototype;

Expand All @@ -57,14 +43,14 @@ class DbSelect implements AdapterInterface
* @throws Exception\InvalidArgumentException
*/
public function __construct(
Select $select,
protected Select $select,
$adapterOrSqlObject,
?ResultSetInterface $resultSetPrototype = null,
?Select $countSelect = null
/**
* Database count query
*/
protected ?Select $countSelect = null
) {
$this->select = $select;
$this->countSelect = $countSelect;

if ($adapterOrSqlObject instanceof Adapter) {
$adapterOrSqlObject = new Sql($adapterOrSqlObject);
}
Expand Down
10 changes: 1 addition & 9 deletions src/Adapter/NullFill.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@

class NullFill implements AdapterInterface
{
/**
* Item count
*
* @var int
*/
protected $count;

/**
* @param int $count Total item count (Optional)
*/
public function __construct($count = 0)
public function __construct(protected $count = 0)
{
$this->count = $count;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Adapter/Service/CallbackFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function createService(ServiceLocatorInterface $container, $name = null,
/**
* Options to use with factory (v2)
*
* @param array $creationOptions
* @return void
*/
public function setCreationOptions(array $creationOptions)
Expand Down
1 change: 0 additions & 1 deletion src/Adapter/Service/DbSelectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function createService(ServiceLocatorInterface $container, $name = null,
/**
* Options to use with factory (v2)
*
* @param array $creationOptions
* @return void
*/
public function setCreationOptions(array $creationOptions)
Expand Down
1 change: 0 additions & 1 deletion src/Adapter/Service/DbTableGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function createService(
/**
* Options to use with factory (v2)
*
* @param array $creationOptions
* @return void
*/
public function setCreationOptions(array $creationOptions)
Expand Down
1 change: 0 additions & 1 deletion src/Adapter/Service/IteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function createService(
/**
* Options to use with factory (v2)
*
* @param array $creationOptions
* @return void
*/
public function setCreationOptions(array $creationOptions)
Expand Down
3 changes: 1 addition & 2 deletions src/AdapterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ public function validate($instance)
/**
* Validate that a plugin is an adapter (v2)
*
* @param mixed $plugin
* @throws Exception\RuntimeException
* @return void
* @psalm-assert AdapterInterface $instance
*/
public function validatePlugin($plugin)
public function validatePlugin(mixed $plugin)
{
try {
$this->validate($plugin);
Expand Down
1 change: 0 additions & 1 deletion src/AdapterPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function createService(ServiceLocatorInterface $container, $name = null,
/**
* laminas-servicemanager v2 support for invocation options.
*
* @param array $options
* @return void
*/
public function setCreationOptions(array $options)
Expand Down
8 changes: 2 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ protected static function createAdapterFromItems($items)
/**
* Get adapter from manager if necessary, and return paginator
*
* @param mixed $items
* @param mixed $adapter
* @return Paginator
*/
protected static function getAdapterFromManager($items, $adapter)
protected static function getAdapterFromManager(mixed $items, mixed $adapter)
{
if ($adapter instanceof AdapterInterface || $adapter instanceof AdapterAggregateInterface) {
return new Paginator($adapter);
Expand All @@ -68,11 +66,9 @@ protected static function getAdapterFromManager($items, $adapter)
/**
* Create paginator with items and adapter
*
* @param mixed $items
* @param mixed $adapter
* @return Paginator
*/
public static function factory($items, $adapter = null)
public static function factory(mixed $items, mixed $adapter = null)
{
if (null === $adapter) {
return static::createAdapterFromItems($items);
Expand Down
20 changes: 10 additions & 10 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use Laminas\Stdlib\ArrayUtils;
use Laminas\View;
use Laminas\View\Renderer\RendererInterface;
use ReturnTypeWillChange; // phpcs:ignore
use ReturnTypeWillChange;
use stdClass;
use Stringable;
use Throwable;
use Traversable;

Expand All @@ -28,6 +29,7 @@
use function count;
use function gettype;
use function is_array;
use function is_countable;
use function is_object;
use function is_string;
use function iterator_count;
Expand All @@ -36,8 +38,8 @@
use function md5;
use function min;
use function sprintf;
use function str_starts_with;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function trigger_error;
Expand All @@ -48,7 +50,7 @@
use const JSON_HEX_QUOT;
use const JSON_HEX_TAG;

class Paginator implements Countable, IteratorAggregate
class Paginator implements Countable, IteratorAggregate, Stringable
{
/**
* The cache tag prefix used to namespace Paginator results in the cache
Expand Down Expand Up @@ -334,10 +336,8 @@ public function __construct($adapter)

/**
* Serializes the object as a string. Proxies to {@link render()}.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
try {
return $this->render();
Expand Down Expand Up @@ -402,7 +402,7 @@ public function clearPageItemCache($pageNumber = null)
$cacheIterator = static::$cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY);
foreach ($cacheIterator as $key) {
if (0 === strpos($key, self::CACHE_TAG_PREFIX)) {
if (str_starts_with($key, self::CACHE_TAG_PREFIX)) {
static::$cache->removeItem($this->_getCacheId((int) substr($key, $prefixLength)));
}
}
Expand Down Expand Up @@ -596,11 +596,11 @@ public function setItemCountPerPage($itemCountPerPage = -1)
* @param mixed $items Items
* @return int
*/
public function getItemCount($items)
public function getItemCount(mixed $items)
{
$itemCount = 0;

if (is_array($items) || $items instanceof Countable) {
if (is_countable($items)) {
$itemCount = count($items);
} elseif ($items instanceof Traversable) { // $items is something like LimitIterator
$itemCount = iterator_count($items);
Expand Down Expand Up @@ -736,7 +736,7 @@ public function getPageItemCache()
$cacheIterator = static::$cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE);
foreach ($cacheIterator as $key => $value) {
if (0 === strpos($key, self::CACHE_TAG_PREFIX)) {
if (str_starts_with($key, self::CACHE_TAG_PREFIX)) {
$data[(int) substr($key, $prefixLength)] = $value;
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/PaginatorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
*/
class PaginatorIterator implements OuterIterator
{
/**
* Internal Paginator for iteration
*
* @var Paginator $paginator
*/
protected $paginator;

/**
* Value for valid method
*
* @var bool $valid
*/
protected $valid = true;

public function __construct(Paginator $paginator)
{
$this->paginator = $paginator;
public function __construct(
/**
* Internal Paginator for iteration
*/
protected Paginator $paginator
) {
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/ScrollingStylePluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ public function validate($instance)
/**
* Validate a plugin (v2)
*
* @param mixed $plugin
* @throws Exception\InvalidArgumentException
* @return void
* @psalm-assert ScrollingStyleInterface $instance
*/
public function validatePlugin($plugin)
public function validatePlugin(mixed $plugin)
{
try {
$this->validate($plugin);
Expand Down
1 change: 0 additions & 1 deletion src/ScrollingStylePluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function createService(ServiceLocatorInterface $container, $name = null,
/**
* laminas-servicemanager v2 support for invocation options.
*
* @param array $options
* @return void
*/
public function setCreationOptions(array $options)
Expand Down
5 changes: 2 additions & 3 deletions src/SerializableLimitIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ public function offsetGet($offset)
* Required by the ArrayAccess implementation
*
* @param int $offset
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
public function offsetSet($offset, mixed $value)
{
}

Expand All @@ -119,7 +118,7 @@ public function offsetExists($offset)
$current = $this->current();
$this->seek($currentOffset);
return null !== $current;
} catch (OutOfBoundsException $e) {
} catch (OutOfBoundsException) {
// reset position in case of exception is assigned null
$this->seek($currentOffset);
return false;
Expand Down
17 changes: 13 additions & 4 deletions test/Adapter/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace LaminasTest\Paginator\Adapter;

use Laminas\Paginator\Adapter;
use Laminas\Paginator\Adapter\ArrayAdapter;
use PHPUnit\Framework\TestCase;

use function assert;
use function range;

/**
Expand All @@ -15,8 +17,7 @@
*/
class ArrayTest extends TestCase
{
/** @var Adapter\ArrayAdapter */
private $adapter;
private ?ArrayAdapter $adapter;

/**
* Prepares the environment before running a test.
Expand All @@ -39,19 +40,27 @@ protected function tearDown(): void
public function testGetsItemsAtOffsetZero(): void
{
$expected = range(1, 10);
$actual = $this->adapter->getItems(0, 10);

assert($this->adapter instanceof ArrayAdapter);

$actual = $this->adapter->getItems(0, 10);
$this->assertEquals($expected, $actual);
}

public function testGetsItemsAtOffsetTen(): void
{
$expected = range(11, 20);
$actual = $this->adapter->getItems(10, 10);

assert($this->adapter instanceof ArrayAdapter);

$actual = $this->adapter->getItems(10, 10);
$this->assertEquals($expected, $actual);
}

public function testReturnsCorrectCount(): void
{
assert($this->adapter instanceof ArrayAdapter);

$this->assertEquals(101, $this->adapter->count());
}

Expand Down
Loading

0 comments on commit 83c3ff8

Please sign in to comment.