Skip to content

Commit

Permalink
enable phpstan level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Oct 3, 2022
1 parent 98fce62 commit 7a64b15
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 482 deletions.
119 changes: 108 additions & 11 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,88 @@ on:
- '[0-9]+.x'

jobs:
php:
name: 'Run tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}'
code-style:
runs-on: ubuntu-latest

steps:
- name: Checkout project
uses: actions/checkout@v3

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: 8.1

- name: Get composer cache directory
id: composer-cache-dir
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
id: composer-cache
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --no-interaction --prefer-dist

- name: Fix code style
run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./

phpstan:
runs-on: ubuntu-latest

steps:
- name: Checkout project
uses: actions/checkout@v3

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: 8.1

- name: Get composer cache directory
id: composer-cache-dir
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
id: composer-cache
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --no-interaction --prefer-dist

- name: Fix code style
run: vendor/bin/phpstan analyse -l3 src/

unit-test:
name: 'Run unit tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}'
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
include:
- php-version: '8.0'
lint: true
symfony-version: '5.4.*'

- php-version: '8.1'
lint: false
symfony-version: '5.4.*'

- php-version: '8.1'
lint: false
symfony-version: '6.0.*'

- php-version: '8.1'
lint: false
symfony-version: '6.1.*'

steps:
Expand Down Expand Up @@ -61,14 +121,51 @@ jobs:
composer require --no-update symfony/serializer:${{ matrix.symfony-version }}
composer install --no-interaction --prefer-dist
- name: Fix code style
if: ${{ matrix.lint }}
run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./

- name: Setup OpenSearch
uses: ankane/setup-opensearch@v1
with:
opensearch-version: '1.2'

- name: Run tests
run: vendor/bin/phpunit --coverage-clover=coverage.xml
run: vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml

functional-test:
name: 'Run functional tests'
runs-on: ubuntu-latest

steps:
- name: Checkout project
uses: actions/checkout@v3

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: 8.1

- name: Get composer cache directory
id: composer-cache-dir
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
id: composer-cache
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: |
composer validate --strict
composer require --no-update symfony/serializer:${{ matrix.symfony-version }}
composer install --no-interaction --prefer-dist
- name: Setup OpenSearch
uses: ankane/setup-opensearch@v1
with:
opensearch-version: '1.2'

- name: Run tests
run: vendor/bin/phpunit --testsuite=functional
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 4 additions & 6 deletions src/Aggregation/AbstractAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OpenSearchDSL\Aggregation;

use OpenSearchDSL\BuilderBag;
use OpenSearchDSL\BuilderInterface;
use OpenSearchDSL\NameAwareTrait;
use OpenSearchDSL\NamedBuilderInterface;
use OpenSearchDSL\ParametersTrait;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function addAggregation(AbstractAggregation $abstractAggregation): static
/**
* Returns all sub aggregations.
*
* @return BuilderBag[]|NamedBuilderInterface[]
* @return BuilderInterface[]
*/
public function getAggregations(): array
{
Expand All @@ -76,11 +77,8 @@ public function getAggregations(): array

/**
* Returns sub aggregation.
* @param string $name Aggregation name to return.
*
* @return AbstractAggregation|NamedBuilderInterface|null
*/
public function getAggregation($name)
public function getAggregation(string $name): ?BuilderInterface
{
if ($this->aggregations && $this->aggregations->has($name)) {
return $this->aggregations->get($name);
Expand Down Expand Up @@ -112,7 +110,7 @@ public function toArray(): array
*
* @return array
*/
protected function collectNestedAggregations()
protected function collectNestedAggregations(): array
{
$result = [];
/** @var AbstractAggregation $aggregation */
Expand Down
102 changes: 20 additions & 82 deletions src/Aggregation/Metric/TopHitsAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,43 @@
*/
class TopHitsAggregation extends AbstractAggregation
{
public BuilderInterface $sort;
use MetricTrait;

/**
* @var int Number of top matching hits to return per bucket.
* @var int|null Number of top matching hits to return per bucket.
*/
private int $size;
private ?int $size;

/**
* @var int The offset from the first result you want to fetch.
* @var int|null The offset from the first result you want to fetch.
*/
private int $from;
private ?int $from;

/**
* @var BuilderInterface[] How the top matching hits should be sorted.
*/
private array $sorts = [];

/**
* Constructor for top hits.
*
* @param string $name Aggregation name.
* @param null|int $size Number of top matching hits to return per bucket.
* @param null|int $from The offset from the first result you want to fetch.
* @param null|BuilderInterface $sort How the top matching hits should be sorted.
*/
public function __construct($name, $size = null, $from = null, $sort = null)
public function __construct(string $name, ?int $size = null, ?int $from = null, ?BuilderInterface $sort = null)
{
parent::__construct($name);
$this->setFrom($from);
$this->setSize($size);
$this->addSort($sort);
}

/**
* Return from.
*
* @return int
*/
public function getFrom()
public function getFrom(): ?int
{
return $this->from;
}

/**
* @param int $from
*
* @return $this
*/
public function setFrom($from)
public function setFrom(?int $from): self
{
$this->from = $from;

Expand All @@ -82,51 +69,36 @@ public function setFrom($from)
/**
* @return BuilderInterface[]
*/
public function getSorts()
public function getSorts(): array
{
return $this->sorts;
}

/**
* @param BuilderInterface[] $sorts
*
* @return $this
*/
public function setSorts(array $sorts)
public function setSorts(array $sorts): self
{
$this->sorts = $sorts;

return $this;
}

/**
* Add sort.
*
* @param BuilderInterface $sort
*/
public function addSort($sort)
public function addSort(BuilderInterface $sort): self
{
$this->sorts[] = $sort;

return $this;
}

/**
* @param int $size
*
* @return $this
*/
public function setSize($size)
public function setSize(?int $size): self
{
$this->size = $size;

return $this;
}

/**
* Return size.
*
* @return int
*/
public function getSize()
public function getSize(): ?int
{
return $this->size;
}
Expand All @@ -139,10 +111,7 @@ public function getType(): string
return 'top_hits';
}

/**
* {@inheritdoc}
*/
public function getArray(): array
public function getArray(): array|stdClass
{
$sortsOutput = [];
$addedSorts = array_filter($this->getSorts());
Expand All @@ -154,43 +123,12 @@ public function getArray(): array
$sortsOutput = null;
}

$output = array_filter(
[
'sort' => $sortsOutput,
'size' => $this->getSize(),
'from' => $this->getFrom(),
],
fn($val) => $val || is_array($val) || ($val || is_numeric($val))
);
$output = array_filter([
'sort' => $sortsOutput,
'size' => $this->getSize(),
'from' => $this->getFrom(),
]);

return empty($output) ? new stdClass() : $output;
}

/**
* @deprecated sorts now is a container, use `getSorts()`instead.
* Return sort.
*
* @return BuilderInterface
*/
public function getSort()
{
if (isset($this->sorts[0])) {
return $this->sorts[0];
}

return null;
}

/**
* @deprecated sorts now is a container, use `addSort()`instead.
*
*
* @return $this
*/
public function setSort(BuilderInterface $sort)
{
$this->sort = $sort;

return $this;
}
}
Loading

0 comments on commit 7a64b15

Please sign in to comment.