Skip to content

Commit

Permalink
chore: add native return types (#366)
Browse files Browse the repository at this point in the history
* chore: add native return types

* fix(ci): add PHP 8 repositories

* fix: syntax

* Update Aggregator.php

* Update Aggregator.php

* chore: add support for symfony v6

* chore: allow only latest versions of Symfony

* chore: drop support for PHP < 8.0

* chore: update QualityTools dependencies

* Update Aggregator.php

* Update Tag.php

* Update Link.php

* chore: linting

* chore: fix static analysis

* fix: lint
  • Loading branch information
DevinCodes authored Jan 27, 2022
1 parent f3af131 commit d1840b8
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 47 deletions.
9 changes: 6 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
steps:
- checkout

- run:
name: Add PHP8 APT repository
command: sudo add-apt-repository ppa:ondrej/php
- run: sudo apt-get update
- run: sudo apt-get install -y php<<parameters.version>>-zip php<<parameters.version>>-sqlite3

Expand Down Expand Up @@ -56,11 +59,11 @@ jobs:
- run:
name: Run static analysis
command: if [[ <<parameters.version>> > "7.3" && <<parameters.version>> < "8.0" ]]; then composer test:install && composer test:types; fi
command: if [[ <<parameters.version>> < "8.1" ]]; then composer test:install && composer test:types; fi

- run:
name: Check code styles
command: if [[ <<parameters.version>> > "7.3" && <<parameters.version>> < "8.0" ]]; then composer test:lint; fi
command: if [[ <<parameters.version>> < "8.1" ]]; then composer test:lint; fi

# Run tests with phpunit
#
Expand All @@ -80,4 +83,4 @@ workflows:
- test:
matrix:
parameters:
version: ['7.2', '7.3', '7.4', '8.0']
version: ['8.0', '8.1']
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
],
"prefer-stable": true,
"require": {
"php": "^7.2 || ^8.0",
"php": ">= 8.0.2",
"algolia/algoliasearch-client-php": "^3.0",
"doctrine/event-manager": "^1.1",
"doctrine/persistence": "^2.1",
"symfony/filesystem": "^3.4 || ^4.0 || ^5.0",
"symfony/property-access": "^3.4 || ^4.0 || ^5.0",
"symfony/serializer": "^3.4 || ^4.0 || ^5.0"
"symfony/filesystem": "^5.0 || ^6.0",
"symfony/property-access": "^5.0 || ^6.0",
"symfony/serializer": "^5.0 || ^6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -40,10 +40,10 @@
"friendsofphp/proxy-manager-lts": "*",
"phpunit/phpunit": "^8.5 || ^9.0",
"roave/security-advisories": "dev-master",
"symfony/framework-bundle": "^3.4 || ^4.0 || ^5.0",
"symfony/phpunit-bridge": "^3.4 || ^4.0 || ^5.0",
"symfony/framework-bundle": "^5.0 || ^6.0",
"symfony/phpunit-bridge": "^5.0 || ^6.0",
"symfony/proxy-manager-bridge": "*",
"symfony/yaml": "^3.4 || ^4.0 || ^5.0"
"symfony/yaml": "^5.0 || ^6.0"
},
"extra": {
"branch-alias": {
Expand All @@ -59,7 +59,7 @@
"lint": "php-cs-fixer fix -v",
"test:types": "tests/QualityTools/vendor/bin/phpstan analyse --ansi",
"test:unit": "XDEBUG_MODE=coverage phpunit --colors=always --verbose",
"test:lint": "php-cs-fixer fix -v --dry-run",
"test:lint": "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix -v --dry-run",
"test": [
"@test:lint",
"@test:unit"
Expand Down
8 changes: 6 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ parameters:
paths:
- src
ignoreErrors:
- '#In (method|function).*type-hinted.*More info#'
- '#In (method|function).*return type can be added..*More info#'
- '#Anonymous function should have native return typehint "*".#'
- '#Language construct isset\(\) should not be used.#'
- '#Class "Algolia\\SearchBundle\\.*" is not allowed to extend ".*".#'
- '#Method Algolia\\SearchBundle\\.*\(\) is not final, but since the containing class is abstract, it should be.#'
- '#File is missing a "declare\(strict_types=1\)" declaration.#'
- '#Method Algolia\\SearchBundle\\DependencyInjection\\AlgoliaSearchExtension::load\(\) has a parameter .* with a type declaration of Symfony\\Component\\DependencyInjection\\ContainerBuilder, but containers should not be injected.#'
- '#(Method|Function|Constructor).*has parameter.*with.*default value.#'
- '#Class Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder constructor#'
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder::root\(\).#'
Expand Down
2 changes: 1 addition & 1 deletion src/Command/SearchClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$indexToClear = $this->getEntitiesFromArgs($input, $output);

Expand Down
4 changes: 2 additions & 2 deletions src/Command/SearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$shouldDoAtomicReindex = (bool) $input->getOption('atomic');
$entitiesToIndex = $this->getEntitiesFromArgs($input, $output);
Expand Down Expand Up @@ -120,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$page++;
$manager->clear();
} while (count($entities) >= $config['batchSize']);
} while (count($entities) >= (int) $config['batchSize']);

$manager->clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/SearchSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(SettingsManager $settingsManager)
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$indexList = $input->getOption('indices');

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$treeBuilder = new TreeBuilder('algolia_search');
Expand Down
7 changes: 4 additions & 3 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public function index($searchableEntities, $requestOptions)
$data[$indexName] = [];
}

$data[$indexName][] = $searchableArray + [
'objectID' => $entity->getId(),
];
$data[$indexName][] = array_merge(
['objectID' => $entity->getId()],
$searchableArray
);
}

$result = [];
Expand Down
15 changes: 13 additions & 2 deletions src/Model/Aggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,20 @@ public static function getEntityClassFromObjectID($objectID)
}

/**
* {@inheritdoc}
* Normalizes the object into an array of scalars|arrays.
*
* It is important to understand that the normalize() call should normalize
* recursively all child objects of the implementor.
*
* @param NormalizerInterface $normalizer The normalizer is given so that you
* can use it to normalize objects contained within this object
* @param string|null $format The format is optionally given to be able to normalize differently
* based on different output formats
* @param array $context Options for normalizing this object
*
* @return array|string|int|float|bool
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []): array|string|int|float|bool
{
return array_merge(['objectID' => $this->objectID], $normalizer->normalize($this->entity, $format, $context));
}
Expand Down
22 changes: 5 additions & 17 deletions src/Responses/SearchServiceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,30 @@ public function wait($requestOptions = [])
}
}

/**
* @return void
*/
public function rewind()
public function rewind(): void
{
$this->position = 0;
}

/**
* @return array<string, AbstractResponse>
*/
public function current()
public function current(): array
{
return $this->apiResponse[$this->key()];
}

/**
* @return int
*/
public function key()
public function key(): int
{
return $this->position;
}

/**
* @return void
*/
public function next()
public function next(): void
{
$this->position++;
}

/**
* @return bool
*/
public function valid()
public function valid(): bool
{
return array_key_exists($this->position, $this->apiResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SearchableEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getIndexName()
}

/**
* @return array<string, int|string|array>
* @return array<string, int|string|array>|void
*
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/QualityTools/composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"require-dev": {
"phpstan/phpstan": "^0.11.5",
"localheinz/phpstan-rules": "^0.6.0",
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan": "^0.12.57",
"localheinz/phpstan-rules": "^0.15.3",
"phpstan/phpstan-strict-rules": "^0.12",
"roave/no-floaters": "^1.1",
"thecodingmachine/phpstan-strict-rules": "^0.11.0"
"thecodingmachine/phpstan-strict-rules": "^0.12.0"
}
}
2 changes: 1 addition & 1 deletion tests/TestApp/Entity/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private function isSponsored()
return false;
}

public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool
{
if (Searchable::NORMALIZATION_FORMAT === $format) {
return [
Expand Down
2 changes: 1 addition & 1 deletion tests/TestApp/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function setPublic($public)
return $this;
}

public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool
{
if (Searchable::NORMALIZATION_FORMAT === $format) {
return [
Expand Down

0 comments on commit d1840b8

Please sign in to comment.