Skip to content

Commit

Permalink
Merge pull request #20 from peter-gribanov/phpstan
Browse files Browse the repository at this point in the history
Test project with PHPStan
  • Loading branch information
peter-gribanov authored Oct 30, 2019
2 parents 5515e93 + 26172e4 commit 7344501
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 99 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ phpunit.xml
composer.lock
.php_cs
.php_cs.cache
phpstan.neon
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ matrix:
env: TWIG_VERSION=1.*
- php: 7.0
env: TWIG_VERSION=2.*
- php: 7.1
env: PHPSTAN_VERSION=0.11.*

before_install:
- if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;
Expand All @@ -52,9 +54,11 @@ before_script:
- if [ "$DOCTRINE_VERSION" != "" ]; then composer require "doctrine/orm:${DOCTRINE_VERSION}" --dev --no-update; fi;
- if [ "$TWIG_VERSION" != "" ]; then composer require "twig/twig:${TWIG_VERSION}" --dev --no-update; fi;
- if [ "$PHPUNIT_VERSION" != "" ]; then composer require "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update; fi;
- if [ "$PHPSTAN_VERSION" != "" ]; then composer require "phpstan/phpstan:${PHPSTAN_VERSION}" --dev --no-update; fi;
- COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction --no-scripts --no-progress

script:
- if [ "$PHPSTAN_VERSION" != "" ]; then ./vendor/bin/phpstan analyse; fi;
- vendor/bin/phpunit --coverage-clover build/coverage-clover.xml

after_script:
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 7
paths:
- src
28 changes: 15 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace GpsLab\Bundle\PaginationBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -30,19 +31,20 @@ public function getConfigTreeBuilder()
$root = $tree_builder->root('gpslab_pagination');
}

$root
->addDefaultsIfNotSet()
->children()
->scalarNode('max_navigate')
->defaultValue(5)
->end()
->scalarNode('parameter_name')
->defaultValue('page')
->end()
->scalarNode('template')
->defaultValue('GpsLabPaginationBundle::pagination.html.twig')
->end()
->end();
// @codeCoverageIgnoreStart
if (!$root instanceof ArrayNodeDefinition) {
throw new \RuntimeException(sprintf(
'Config root node must be a "%s", given "%s".',
'Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition',
get_class($root)
));
}
// @codeCoverageIgnoreEnd

$root->addDefaultsIfNotSet();
$root->children()->scalarNode('max_navigate')->defaultValue(5);
$root->children()->scalarNode('parameter_name')->defaultValue('page');
$root->children()->scalarNode('template')->defaultValue('GpsLabPaginationBundle::pagination.html.twig');

return $tree_builder;
}
Expand Down
13 changes: 4 additions & 9 deletions src/Service/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace GpsLab\Bundle\PaginationBundle\Service;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use GpsLab\Bundle\PaginationBundle\Exception\IncorrectPageNumberException;
use GpsLab\Bundle\PaginationBundle\Exception\OutOfRangeException;
Expand Down Expand Up @@ -70,8 +69,6 @@ public function paginate($total_pages = 1, $current_page = 1)
* @param int $per_page Entities per page
* @param int $current_page The current page number
*
* @throws NonUniqueResultException
*
* @return Configuration
*/
public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1)
Expand All @@ -83,7 +80,7 @@ public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1)
->getSingleScalarResult()
;

$total_pages = ceil($total / $per_page);
$total_pages = (int) ceil($total / $per_page);
$current_page = $this->validateCurrentPage($current_page, $total_pages);

$query
Expand Down Expand Up @@ -126,8 +123,6 @@ public function paginateRequest(
* @param string $parameter_name Name of URL parameter for page number
* @param int $reference_type The type of reference (one of the constants in UrlGeneratorInterface)
*
* @throws NonUniqueResultException
*
* @return Configuration
*/
public function paginateRequestQuery(
Expand Down Expand Up @@ -159,12 +154,12 @@ private function validateCurrentPage($current_page, $total_pages)
return 1;
}

if (!is_numeric($current_page)) {
if (!is_int($current_page) && (!is_string($current_page) || !ctype_digit($current_page))) {
throw IncorrectPageNumberException::incorrect($current_page);
}

if ($current_page < 1 || $current_page > $total_pages) {
throw OutOfRangeException::out($current_page, $total_pages);
throw OutOfRangeException::out((int) $current_page, $total_pages);
}

return (int) $current_page;
Expand All @@ -185,7 +180,7 @@ private function configureFromRequest(
$reference_type
) {
$route = $request->get('_route');
$route_params = array_merge($request->query->all(), $request->get('_route_params'));
$route_params = array_merge($request->query->all(), $request->get('_route_params', []));
unset($route_params[$parameter_name]);

return $configuration
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Configuration
private $current_page = 1;

/**
* @var View
* @var View|null
*/
private $view;

Expand Down
12 changes: 6 additions & 6 deletions src/Service/NavigateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ private function definitionOffset()
private function adjustmentLargeLeftOffset()
{
if ($this->config->getCurrentPage() - $this->left_offset < 1) {
$offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset);
$this->left_offset = $this->left_offset - $offset;
$this->right_offset = $this->right_offset + $offset;
$offset = abs($this->config->getCurrentPage() - 1 - $this->left_offset);
$this->left_offset -= $offset;
$this->right_offset += $offset;
}
}

Expand All @@ -93,13 +93,13 @@ private function adjustmentLargeLeftOffset()
private function adjustmentLargeRightOffset()
{
if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) {
$offset = (int) abs(
$offset = abs(
$this->config->getTotalPages() -
$this->config->getCurrentPage() -
$this->right_offset
);
$this->left_offset = $this->left_offset + $offset;
$this->right_offset = $this->right_offset - $offset;
$this->left_offset += $offset;
$this->right_offset -= $offset;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Service/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class View implements \IteratorAggregate
private $prev;

/**
* @var Node
* @var Node|null
*/
private $current;

Expand Down Expand Up @@ -144,7 +144,7 @@ public function getLast()
}

/**
* @return ArrayCollection
* @return ArrayCollection|Node[]
*/
public function getIterator()
{
Expand Down
92 changes: 36 additions & 56 deletions tests/Service/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use GpsLab\Bundle\PaginationBundle\Service\Builder;
use GpsLab\Bundle\PaginationBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand All @@ -26,11 +25,6 @@ class BuilderTest extends TestCase
*/
private $router;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|Request
*/
private $request;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|AbstractQuery
*/
Expand All @@ -41,6 +35,11 @@ class BuilderTest extends TestCase
*/
private $query_builder;

/**
* @var Request
*/
private $request;

/**
* @var array
*/
Expand All @@ -52,11 +51,10 @@ class BuilderTest extends TestCase
protected function setUp()
{
$this->router = $this->getMockNoConstructor('Symfony\Bundle\FrameworkBundle\Routing\Router');
$this->request = $this->getMockNoConstructor('Symfony\Component\HttpFoundation\Request');
$this->query = $this->getMockAbstract('Doctrine\ORM\AbstractQuery', ['getSingleScalarResult']);
$this->query_builder = $this->getMockNoConstructor('Doctrine\ORM\QueryBuilder');

$this->request->query = new ParameterBag($this->query_params);
$this->request = new Request($this->query_params);
}

public function testDefaultPageLink()
Expand Down Expand Up @@ -158,7 +156,9 @@ public function testPaginateQueryOutOfRange()
*/
public function testPaginateRequestIncorrectPage()
{
$this->currentPage('foo', 'page');
$this->request = new Request(array_merge($this->query_params, [
'page' => 'foo',
]));

$builder = new Builder($this->router, 5, 'page');
$builder->paginateRequest($this->request, 10);
Expand All @@ -169,7 +169,9 @@ public function testPaginateRequestIncorrectPage()
*/
public function testPaginateRequestLowPageNumber()
{
$this->currentPage(0, 'p');
$this->request = new Request(array_merge($this->query_params, [
'p' => 0,
]));

$builder = new Builder($this->router, 5, 'page');
$builder->paginateRequest($this->request, 10, 'p');
Expand All @@ -180,7 +182,9 @@ public function testPaginateRequestLowPageNumber()
*/
public function testPaginateRequestOutOfRange()
{
$this->currentPage(150, 'p');
$this->request = new Request(array_merge($this->query_params, [
'p' => 150,
]));

$builder = new Builder($this->router, 5, 'page');
$builder->paginateRequest($this->request, 10, 'p');
Expand All @@ -195,20 +199,12 @@ public function testPaginateRequest()
$route_params = ['foo' => 'baz', '_route_params' => 123];
$all_params = array_merge($this->query_params, $route_params);
$reference_type = UrlGeneratorInterface::ABSOLUTE_URL;

$this->currentPage(null, 'p');
$this->request
->expects($this->at(1))
->method('get')
->with('_route')
->willReturn($route)
;
$this->request
->expects($this->at(2))
->method('get')
->with('_route_params')
->willReturn($route_params)
;
$this->request = new Request(array_merge($this->query_params, [
'p' => null,
]), [], [
'_route' => $route,
'_route_params' => $route_params,
]);

$that = $this;
$this->router
Expand Down Expand Up @@ -247,7 +243,9 @@ public function testPaginateRequest()
*/
public function testPaginateRequesQuerytIncorrectPage()
{
$this->currentPage('foo', 'page');
$this->request = new Request(array_merge($this->query_params, [
'page' => 'foo',
]));
$this->countQuery(10);

$builder = new Builder($this->router, 5, 'page');
Expand All @@ -259,7 +257,9 @@ public function testPaginateRequesQuerytIncorrectPage()
*/
public function testPaginateRequestQueryLowPageNumber()
{
$this->currentPage(0, 'p');
$this->request = new Request(array_merge($this->query_params, [
'p' => 0,
]));
$this->countQuery(10);

$builder = new Builder($this->router, 5, 'page');
Expand All @@ -271,7 +271,9 @@ public function testPaginateRequestQueryLowPageNumber()
*/
public function testPaginateRequestQueryOutOfRange()
{
$this->currentPage(150, 'p');
$this->request = new Request(array_merge($this->query_params, [
'p' => 150,
]));
$this->countQuery(10);

$builder = new Builder($this->router, 5, 'page');
Expand All @@ -289,20 +291,12 @@ public function testPaginateRequestQuery()
$route_params = ['foo' => 'baz', '_route_params'];
$all_params = array_merge($this->query_params, $route_params);
$reference_type = UrlGeneratorInterface::ABSOLUTE_URL;

$this->currentPage($current_page, 'p');
$this->request
->expects($this->at(1))
->method('get')
->with('_route')
->willReturn($route)
;
$this->request
->expects($this->at(2))
->method('get')
->with('_route_params')
->willReturn($route_params)
;
$this->request = new Request(array_merge($this->query_params, [
'p' => $current_page,
]), [], [
'_route' => $route,
'_route_params' => $route_params,
]);

$this->countQuery($total);
$this->query_builder
Expand Down Expand Up @@ -356,20 +350,6 @@ public function testPaginateRequestQuery()
);
}

/**
* @param int $current_page
* @param string $parameter_name
*/
private function currentPage($current_page, $parameter_name = 'page')
{
$this->request
->expects($this->at(0))
->method('get')
->with($parameter_name)
->willReturn($current_page)
;
}

/**
* @param int $total
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Service/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConfigurationTest extends TestCase
/**
* @var Configuration
*/
protected $config;
private $config;

protected function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Service/NavigateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class NavigateRangeTest extends TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject|Configuration
*/
protected $config;
private $config;

/**
* @var NavigateRange
*/
protected $range;
private $range;

protected function setUp()
{
Expand Down
Loading

0 comments on commit 7344501

Please sign in to comment.