Skip to content

Commit

Permalink
feat: add code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit382 committed Jul 11, 2024
1 parent 20c7494 commit a738b25
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 84 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ jobs:
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Check quality code
env:
PHP_CS_FIXER_IGNORE_ENV: 1
run: vendor/bin/php-cs-fixer fix --ansi --dry-run --using-cache=no --verbose
- name: Execute phpstan
run: vendor/bin/phpstan
- name: Execute tests
run: vendor/bin/phpunit --coverage-clover ./build/coverage.clover
- name: Uploading code coverage to scrutinize
uses: sudo-bot/action-scrutinizer@latest
with:
cli-args: "--format=php-clover build/coverage.clover"
cli-args: "--format=php-clover build/coverage.clover"
22 changes: 22 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$config = new PhpCsFixer\Config();
$finder = $config->getFinder()
->in(['src','tests',])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return $config
->setRules([
'@PSR12' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'single_quote' => true,
'blank_line_before_statement' => true,
'no_spaces_around_offset' => true,
'no_unused_imports' => true,
'ternary_operator_spaces' => true,
])
->setUsingCache(false);
40 changes: 0 additions & 40 deletions .php_cs.dist.php

This file was deleted.

20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


.PHONY: ci
ci: cs-check phpstan test

.PHONY: cs-check
cs-check:
php ./vendor/bin/php-cs-fixer check

.PHONY: cs-fix
cs-fix:
php ./vendor/bin/php-cs-fixer fix

.PHONY: test
test:
php ./vendor/bin/phpunit

.PHONY: phpstan
phpstan:
php ./vendor/bin/phpstan
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.9.5",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5.21|^10.5"
},
"autoload": {
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/larastan/larastan/extension.neon

parameters:
level: 9
checkMissingIterableValueType: false
paths:
- src
- tests
ignoreErrors:
-
message: "#^Cannot access offset '[a-z.]+' on Illuminate\\\\Contracts\\\\Container\\\\Container.$#"
path: src/OpenFoodFacts.php
8 changes: 7 additions & 1 deletion src/Facades/OpenFoodFacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
namespace OpenFoodFacts\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Collection;
use OpenFoodFacts\Document;

/**
* @method static array barcode(string $value)
* @method static \Illuminate\Support\Collection find(string $searchterm)
* @method static Collection find(string $searchTerm)
* @method static Document getProduct(string $barcode)
*
* @see \OpenFoodFacts\Laravel\OpenFoodFacts
*/
class OpenFoodFacts extends Facade
{
/**
* @inheritDoc
*/
protected static function getFacadeAccessor()
{
return 'openfoodfacts';
Expand Down
29 changes: 17 additions & 12 deletions src/OpenFoodFacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use OpenFoodFacts\Api;
use OpenFoodFacts\Document;
use OpenFoodFacts\Exception\ProductNotFoundException;

/** @mixin \OpenFoodFacts\Api */
/** @mixin Api */
class OpenFoodFacts extends OpenFoodFactsApiWrapper
{
protected $max_results;
protected int $max_results;

public function __construct(Container $app, string $geography = null)
{
parent::__construct([
'geography' => $geography ?? $app['config']->get('openfoodfacts.geography'),
'app' => $app['config']->get('app.name'),
'geography' => $geography ?? $app['config']->get('openfoodfacts.geography'),
'app' => $app['config']->get('app.name'),
], $app['cache.store']);

$this->max_results = $app['config']->get('openfoodfacts.max_results', 1000);
Expand All @@ -31,14 +33,14 @@ public function __construct(Container $app, string $geography = null)
public function barcode(string $value): array
{
if (empty($value)) {
throw new InvalidArgumentException("Argument must represent a barcode");
throw new InvalidArgumentException('Argument must represent a barcode');
}

try {
$doc = $this->api->getProduct($value);

return empty($doc->code) ? [] : reset($doc);
} catch (ProductNotFoundException $notFoundException) {
return empty($doc->code) ? [] : (array) reset($doc);
} catch (ProductNotFoundException) {
return [];
}
}
Expand All @@ -47,14 +49,15 @@ public function barcode(string $value): array
* Search products by term
*
* @param string $searchterm
* @return Collection<array>
* @return Collection<int, array>
*/
public function find(string $searchterm): Collection
{
if (empty($searchterm)) {
throw new InvalidArgumentException("Specify a search term to find data for matching products");
throw new InvalidArgumentException('Specify a search term to find data for matching products');
}

/** @var Collection<int, Document> $products */
$products = Collection::make();
$page = 0;

Expand All @@ -67,16 +70,18 @@ public function find(string $searchterm): Collection
}

$pages = (int)ceil($totalMatches / $pageResults->getPageSize());
/** @var Document[] $array */
$array = iterator_to_array($pageResults);

$products = $products->concat(iterator_to_array($pageResults));
$products = $products->concat($array);
} while ($page < $pages);

return $products->map(function ($product) {

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.1 - Laravel 10.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.1 - Laravel 9.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 11.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 10.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 9.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.1 - Laravel 10.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.1 - Laravel 9.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 11.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 10.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.

Check failure on line 79 in src/OpenFoodFacts.php

View workflow job for this annotation

GitHub Actions / Tests - PHP 8.2 - Laravel 9.* - prefer-stable

Method OpenFoodFacts\Laravel\OpenFoodFacts::find() should return Illuminate\Support\Collection<int, array> but returns Illuminate\Support\Collection<int, array{bool}>.
return reset($product);
return (array) reset($product);
});
}

public function __call($method, $parameters)
public function __call(string $method, array $parameters): mixed
{
return $this->api->$method(...$parameters);
}
Expand Down
17 changes: 7 additions & 10 deletions src/OpenFoodFactsApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@

class OpenFoodFactsApiWrapper
{
protected $parameters;
protected $cache;
public readonly Api $api;

public $api;

public function __construct(array $parameters, CacheInterface $cache = null)
{
$this->parameters = $parameters;
$this->cache = $cache;
public function __construct(
public readonly array $parameters,
protected readonly ?CacheInterface $cache = null
) {

$this->api = $this->setupApi();
}

protected function setupApi($environment = 'food')
protected function setupApi(string $environment = 'food'): Api
{
return new Api(
$environment,
Expand All @@ -32,7 +29,7 @@ protected function setupApi($environment = 'food')
);
}

protected function httpClient()
protected function httpClient(): Client
{
return new Client([
'headers' => ['User-Agent' => $this->parameters['app'] ?? 'Laravel Open Food Facts - https://github.com/openfoodfacts/openfoodfacts-laravel'],
Expand Down
5 changes: 4 additions & 1 deletion src/OpenFoodFactsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class OpenFoodFactsServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand All @@ -16,6 +16,9 @@ public function boot()
}
}

/**
* @inheritDoc
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/openfoodfacts.php', 'openfoodfacts');
Expand Down
11 changes: 5 additions & 6 deletions tests/BarcodeFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@
class BarcodeFindTest extends Base\FacadeTestCase
{
/** @test */
public function it_returns_an_array_with_data_when_product_found()
public function it_returns_an_array_with_data_when_product_found(): void
{
$arr = OpenFoodFacts::barcode('0737628064502');

$this->assertArrayHasKey('code', $arr);
}

/** @test */
public function it_returns_an_empty_array_when_product_not_found()
public function it_returns_an_empty_array_when_product_not_found(): void
{
$arr = OpenFoodFacts::barcode('this-barcode-does-not-exist');

$this->assertEquals([], $arr);
}

/** @test */
public function it_throws_an_exception_when_argument_empty()
public function it_throws_an_exception_when_argument_empty(): void
{
$this->expectException("InvalidArgumentException");
$this->expectExceptionMessage("Argument must represent a barcode");
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Argument must represent a barcode');

OpenFoodFacts::barcode('');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Base/FacadeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace OpenFoodFacts\Laravel\Tests\Base;

use Orchestra\Testbench\TestCase;
use OpenFoodFacts\Laravel\OpenFoodFactsServiceProvider;
use Orchestra\Testbench\TestCase;

abstract class FacadeTestCase extends TestCase
{
protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [OpenFoodFactsServiceProvider::class];
}
Expand Down
5 changes: 3 additions & 2 deletions tests/FacadeBridgeToApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace OpenFoodFacts\Laravel\Tests;

use OpenFoodFacts\Document;
use OpenFoodFacts\Laravel\Facades\OpenFoodFacts;

class FacadeBridgeToApiTest extends Base\FacadeTestCase
{
/** @test */
public function it_calls_method_on_vendor_apiclass()
public function it_calls_method_on_vendor_apiclass(): void
{
$doc = OpenFoodFacts::getProduct('0737628064502');

$this->assertInstanceOf(\OpenFoodFacts\Document::class, $doc);
$this->assertInstanceOf(Document::class, $doc);
}
}
4 changes: 2 additions & 2 deletions tests/GeographyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class GeographyTest extends Base\FacadeTestCase
{
/** @test */
public function it_returns_different_content_based_on_geography_parameter()
public function it_returns_different_content_based_on_geography_parameter(): void
{
$barcode = '8714200216964';

Expand All @@ -24,7 +24,7 @@ public function it_returns_different_content_based_on_geography_parameter()
}

/** @test */
public function it_returns_geo_specific_content_through_config_setting()
public function it_returns_geo_specific_content_through_config_setting(): void
{
Config::set('openfoodfacts.geography', 'nl');
$product_dutch_content = OpenFoodFactsFacade::barcode('8714200216964');
Expand Down
Loading

0 comments on commit a738b25

Please sign in to comment.