Skip to content

Commit

Permalink
Added PHP 8 support; Removed support for PHP 7.1; Added more return t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
korridor committed Nov 2, 2021
1 parent 9d2ee4c commit cbabc42
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 57 deletions.
11 changes: 2 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ language: php

matrix:
include:
# Laravel 5.7.*
- php: 7.1
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml.old'
- php: 7.2
env: LARAVEL='5.7.*' TESTBENCH='3.7.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml.old'
# Laravel 5.8.*
- php: 7.1
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml.old'
- php: 7.2
env: LARAVEL='5.8.*' TESTBENCH='3.8.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml.old'
- php: 7.3
Expand All @@ -31,14 +24,14 @@ matrix:
- php: 7.4
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml'
# Laravel 8.*
- php: 7.3
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-lowest' PHP_UNIT_CONFIG='phpunit.xml'
- php: 7.3
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml'
- php: 7.4
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-lowest' PHP_UNIT_CONFIG='phpunit.xml'
- php: 7.4
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml'
- php: 8.0
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable' PHP_UNIT_CONFIG='phpunit.xml'
fast_finish: true

before_install:
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "korridor/laravel-computed-attributes",
"description": "Laravel package that adds computed attributes to eloquent models.",
"keywords": ["laravel", "model", "eloquent", "computed", "attribute", "caching", "performance"],
"description": "Laravel package that adds computed attributes to eloquent models. A computed attribute is an accessor were the computed value is saved in the database.",
"keywords": ["laravel", "model", "eloquent", "computed", "attribute", "caching", "performance", "accessor"],
"homepage": "https://github.com/korridor/laravel-computed-attributes",
"license": "MIT",
"authors": [
Expand All @@ -12,11 +12,11 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.1",
"php": "^7.2|^8.0",
"composer/composer": "^1.10 || ^2",
"illuminate/console": "^5.7|^6|^7|^8",
"illuminate/database": "^5.7|^6|^7|^8",
"illuminate/support": "^5.7|^6|^7|^8"
"illuminate/console": "^5.8|^6|^7|^8",
"illuminate/database": "^5.8|^6|^7|^8",
"illuminate/support": "^5.8|^6|^7|^8"
},
"require-dev": {
"orchestra/testbench": "^3.6|^4.0|^5.0|^6.0",
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM php:7.4-cli
FROM php:8.0-cli

RUN apt-get update && apt-get install -y \
zlib1g-dev \
libzip-dev

RUN docker-php-ext-install zip

RUN pecl install xdebug-2.8.1 \
RUN pecl install xdebug-3.0.4 \
&& docker-php-ext-enable xdebug

# Install composer and add its bin to the PATH.
Expand Down
4 changes: 1 addition & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ composer require korridor/laravel-computed-attributes

This package is tested for the following Laravel versions:

- 8.* (PHP 7.3, 7.4)
- 8.* (PHP 7.3, 7.4, 8.0)
- 7.* (PHP 7.2, 7.3, 7.4)
- 6.* (PHP 7.2, 7.3)
- 5.8.* (PHP 7.1, 7.2, 7.3)
- 5.7.* (PHP 7.1, 7.2, 7.3, stable only)

## Usage examples

Expand Down
12 changes: 6 additions & 6 deletions src/ComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait ComputedAttributes
/**
* Compute the given attribute and return the result.
*
* @param string $attributeName
* @param string $attributeName
* @return mixed
*/
public function getComputedAttributeValue(string $attributeName)
Expand All @@ -28,7 +28,7 @@ public function getComputedAttributeValue(string $attributeName)
/**
* Compute the given attribute and assign the result in the model.
*
* @param string $attributeName
* @param string $attributeName
*/
public function setComputedAttributeValue(string $attributeName): void
{
Expand All @@ -39,8 +39,8 @@ public function setComputedAttributeValue(string $attributeName): void
/**
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
*
* @param Builder $builder
* @param array $attributes Attributes that will be generated.
* @param Builder $builder
* @param array $attributes Attributes that will be generated.
* @return Builder
*/
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
Expand All @@ -51,8 +51,8 @@ public function scopeComputedAttributesGenerate(Builder $builder, array $attribu
/**
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
*
* @param Builder $builder
* @param array $attributes Attributes that will be validated.
* @param Builder $builder
* @param array $attributes Attributes that will be validated.
* @return Builder
*/
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
Expand Down
4 changes: 3 additions & 1 deletion src/Console/GenerateComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class GenerateComputedAttributes extends Command
* Execute the console command.
*
* @return int
*
* @throws ReflectionException
*/
public function handle()
public function handle(): int
{
$modelsWithAttributes = $this->argument('modelsAttributes');

Expand All @@ -61,6 +62,7 @@ public function handle()
}

// Validate and parse modelsAttributes argument
/** @var ModelAttributeParser $modelAttributeParser */
$modelAttributeParser = app(ModelAttributeParser::class);
try {
$modelAttributesEntries = $modelAttributeParser->getModelAttributeEntries($modelsWithAttributes);
Expand Down
8 changes: 5 additions & 3 deletions src/Console/ValidateComputedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class ValidateComputedAttributes extends Command
* Execute the console command.
*
* @return int
*
* @throws ReflectionException
*/
public function handle()
public function handle(): int
{
$modelsWithAttributes = $this->argument('modelsAttributes');

Expand All @@ -61,6 +62,7 @@ public function handle()
}

// Validate and parse modelsAttributes argument
/** @var ModelAttributeParser $modelAttributeParser */
$modelAttributeParser = app(ModelAttributeParser::class);
try {
$modelAttributesEntries = $modelAttributeParser->getModelAttributeEntries($modelsWithAttributes);
Expand Down Expand Up @@ -103,9 +105,9 @@ public function handle()

/**
* @param $var
* @return false|string
* @return string
*/
private function varToString($var)
private function varToString($var): string
{
if ($var === null) {
return 'null';
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelComputedAttributesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class LaravelComputedAttributesServiceProvider extends ServiceProvider
/**
* Register services.
*/
public function register()
public function register(): void
{
}

/**
* Bootstrap services.
*/
public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand Down
9 changes: 6 additions & 3 deletions src/Parser/ModelAttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public function getModelNamespaceBase(): string

/**
* Get all models classes that use the ComputedAttributes trait.
*
* @return array
*
* @throws ReflectionException
*/
public function getAllModelClasses()
public function getAllModelClasses(): array
{
// Get all models with trait
$classmap = ClassMapGenerator::createMap($this->getAbsolutePathOfModelFolder());
Expand All @@ -58,12 +60,13 @@ public function getAllModelClasses()
}

/**
* @param string|null $modelsWithAttributes
* @param string|null $modelsWithAttributes
* @return ModelAttributesEntry[]
*
* @throws ParsingException
* @throws ReflectionException
*/
public function getModelAttributeEntries(?string $modelsWithAttributes = null)
public function getModelAttributeEntries(?string $modelsWithAttributes = null): array
{
$modelAttributesToProcess = [];
$models = $this->getAllModelClasses();
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/ModelAttributesEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class ModelAttributesEntry

/**
* ModelAttributesEntry constructor.
* @param string $model
* @param string[] $attributes
*
* @param string $model
* @param string[] $attributes
*/
public function __construct(string $model, array $attributes)
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/GenerateComputedAttributesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GenerateComputedAttributesCommandTest extends TestCase
{
use RefreshDatabase;

public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes()
public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void
{
// Arrange
$post = new Post();
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAtt
]);
}

public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInArgument()
public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInArgument(): void
{
// Arrange
$post = new Post();
Expand Down Expand Up @@ -92,7 +92,7 @@ public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInAr
]);
}

public function testNonNumericChunkSizeIsReturnsErrorMessage()
public function testNonNumericChunkSizeIsReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:generate', [
'--chunkSize' => 'text',
Expand All @@ -102,7 +102,7 @@ public function testNonNumericChunkSizeIsReturnsErrorMessage()
->execute();
}

public function testNegativeChunkSizeReturnsErrorMessage()
public function testNegativeChunkSizeReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:generate', [
'--chunkSize' => '-10',
Expand All @@ -112,7 +112,7 @@ public function testNegativeChunkSizeReturnsErrorMessage()
->execute();
}

public function testZeroAsChunkSizeReturnsErrorMessage()
public function testZeroAsChunkSizeReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:generate', [
'--chunkSize' => '0',
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/ValidateComputedAttributesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ValidateComputedAttributesCommandTest extends TestCase
{
use RefreshDatabase;

public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes()
public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAttributes(): void
{
// Arrange
$post = new Post();
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testCommandComputesAttributesForAllModelsWithTraitAndAllThereAtt
]);
}

public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInArgument()
public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInArgument(): void
{
// Arrange
$post = new Post();
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testCommandCanOnlyCalculateOneAttributeOfOneModelIfSpecifiedInAr
]);
}

public function testNonNumericChunkSizeIsReturnsErrorMessage()
public function testNonNumericChunkSizeIsReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:validate', [
'--chunkSize' => 'text',
Expand All @@ -111,7 +111,7 @@ public function testNonNumericChunkSizeIsReturnsErrorMessage()
->execute();
}

public function testNegativeChunkSizeReturnsErrorMessage()
public function testNegativeChunkSizeReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:validate', [
'--chunkSize' => '-10',
Expand All @@ -121,7 +121,7 @@ public function testNegativeChunkSizeReturnsErrorMessage()
->execute();
}

public function testZeroAsChunkSizeReturnsErrorMessage()
public function testZeroAsChunkSizeReturnsErrorMessage(): void
{
$this->artisan('computed-attributes:validate', [
'--chunkSize' => '0',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected function getEnvironmentSetUp($app)
}

/**
* @param Application $app
* @param Application $app
* @return array
*/
protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [
LaravelComputedAttributesServiceProvider::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AddPostsTable extends Migration
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand All @@ -29,7 +29,7 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('posts');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AddVotesTable extends Migration
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('votes', function (Blueprint $table) {
$table->increments('id');
Expand All @@ -32,7 +32,7 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('votes');
}
Expand Down
8 changes: 4 additions & 4 deletions tests/TestEnvironment/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function getSumOfVotesComputed(): int
/**
* This scope will be applied during the computed property generation with artisan computed-attributes:generate.
*
* @param Builder $builder
* @param array $attributes Attributes that will be generated.
* @param Builder $builder
* @param array $attributes Attributes that will be generated.
* @return Builder
*/
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
Expand All @@ -75,8 +75,8 @@ public function scopeComputedAttributesGenerate(Builder $builder, array $attribu
/**
* This scope will be applied during the computed property validation with artisan computed-attributes:validate.
*
* @param Builder $builder
* @param array $attributes Attributes that will be validated.
* @param Builder $builder
* @param array $attributes Attributes that will be validated.
* @return Builder
*/
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
Expand Down

0 comments on commit cbabc42

Please sign in to comment.