From 9f9b3062c725b68ac999f3e35ac050304a3c52fb Mon Sep 17 00:00:00 2001 From: Viacheslav Ostrovskii Date: Wed, 15 Feb 2023 21:35:30 +0200 Subject: [PATCH 1/3] Laravel 10 --- .github/workflows/tests.yml | 2 +- .gitignore | 2 + .php-cs-fixer.php | 304 ++++++++++++++++++++++ composer.json | 15 +- phpunit.xml.dist | 32 +-- src/BelongsTo.php | 5 +- src/BelongsToMany.php | 19 +- src/Concerns/HasBelongsToEvents.php | 43 ++- src/Concerns/HasBelongsToManyEvents.php | 70 ++--- src/Concerns/HasManyEvents.php | 39 ++- src/Concerns/HasMorphManyEvents.php | 41 ++- src/Concerns/HasMorphOneEvents.php | 41 ++- src/Concerns/HasMorphToEvents.php | 45 ++-- src/Concerns/HasMorphToManyEvents.php | 99 ++++--- src/Concerns/HasMorphedByManyEvents.php | 99 ++++--- src/Concerns/HasOneEvents.php | 39 ++- src/Contracts/EventDispatcher.php | 2 - src/Helpers/AttributesMethods.php | 16 +- src/MorphMany.php | 4 +- src/MorphTo.php | 5 +- src/MorphToMany.php | 17 +- src/MorphedByMany.php | 17 +- src/Traits/HasDispatchableEvents.php | 9 +- src/Traits/HasEventDispatcher.php | 2 - src/Traits/HasOneOrManyMethods.php | 22 +- src/Traits/HasRelationshipObservables.php | 19 +- 26 files changed, 641 insertions(+), 367 deletions(-) create mode 100644 .php-cs-fixer.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 964a5aa..7d0f85d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4, 8.0] + php: [8.1, 8.2] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - S${{ matrix.stability }} diff --git a/.gitignore b/.gitignore index ef9d7cb..fa3d491 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ yarn-error.log .env .DS_Store /composer.lock +/.phpunit.cache +/.phpunit.result.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..3952e61 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,304 @@ +setUsingCache(false); + +$finder = PhpCsFixer\Finder::create() + ->exclude([ + 'admin/components/gii/crud/default', + 'admin/module/rbac/views', + 'admin/views', + 'api/docs', + 'common/components/cards/protobuf', + 'common/components/iban/protobuf', + 'common/components/walletApi/protobuf', + 'console/views', + 'partners/views', + 'partners/views_old', + 'tests', + ]) + ->in(__DIR__); + +return $config->setRules([ + '@PSR12' => true, + '@PHP82Migration' => true, + 'array_syntax' => ['syntax' => 'short'], + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => [ + 'break', + 'continue', + 'declare', + 'exit', + 'return', + 'throw', + 'try', + 'yield', + 'yield_from', + ], + ], + 'braces' => ['allow_single_line_anonymous_class_with_empty_body' => true], + 'cast_spaces' => true, + 'class_attributes_separation' => [ + 'elements' => [ + 'method' => 'one', + ], + ], + 'class_definition' => ['multi_line_extends_each_single_line' => true], + 'class_reference_name_casing' => true, + 'clean_namespace' => true, + 'concat_space' => ['spacing' => 'one'], + 'echo_tag_syntax' => ['format' => 'short'], + 'empty_loop_body' => ['style' => 'braces'], + 'empty_loop_condition' => true, + 'fully_qualified_strict_types' => true, + 'function_typehint_space' => true, + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => false, + ], + 'include' => true, + 'integer_literal_case' => true, + 'lambda_not_used_import' => true, + 'linebreak_after_opening_tag' => true, + 'magic_constant_casing' => true, + 'magic_method_casing' => true, + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => false, + 'after_heredoc' => true, + ], + 'native_function_casing' => true, + 'native_function_type_declaration_casing' => true, + 'no_alias_language_construct_call' => true, + 'no_alternative_syntax' => ['fix_non_monolithic_code' => true], + 'no_binary_string' => true, + 'no_blank_lines_after_phpdoc' => true, + 'no_empty_comment' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'attribute', + 'break', + 'case', + 'continue', + 'curly_brace_block', + 'default', + 'extra', + 'parenthesis_brace_block', + 'return', + 'square_brace_block', + 'switch', + 'throw', + 'use', + ], + ], + 'no_leading_namespace_whitespace' => true, + 'no_mixed_echo_print' => true, + 'no_multiline_whitespace_around_double_arrow' => true, + 'no_short_bool_cast' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_spaces_around_offset' => true, + 'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'allow_unused_params' => true], // Убирать лишние теги из phpdoc + 'no_trailing_comma_in_singleline' => [ + 'elements' => [ + 'arguments', + 'array_destructuring', + 'array', + 'group_import', + ], + ], + 'no_unneeded_control_parentheses' => [ + 'statements' => [ + 'break', + 'clone', + 'continue', + 'echo_print', + 'negative_instanceof', + 'others', + 'return', + 'switch_case', + 'yield', + 'yield_from', + ], + ], + 'no_unneeded_curly_braces' => ['namespaces' => true], + 'no_unneeded_import_alias' => true, + 'no_unset_cast' => true, + 'no_unused_imports' => true, + 'no_useless_concat_operator' => true, + 'no_useless_nullsafe_operator' => true, + 'no_whitespace_before_comma_in_array' => ['after_heredoc' => true], + 'normalize_index_brace' => true, + 'object_operator_without_whitespace' => true, + 'ordered_imports' => [ + 'imports_order' => [ + 'class', + 'function', + 'const', + ], + 'sort_algorithm' => 'alpha', + ], + 'php_unit_fqcn_annotation' => true, + 'php_unit_method_casing' => true, + 'phpdoc_align' => ['align' => 'left'], + 'phpdoc_annotation_without_dot' => true, + 'phpdoc_indent' => true, + 'phpdoc_no_access' => true, + 'phpdoc_no_alias_tag' => [ + 'replacements' => [ + 'type' => 'var', + 'link' => 'see', + ], + ], + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_order' => [ + 'order' => [ + 'param', + 'return', + 'throws', + ], + ], + 'phpdoc_return_self_reference' => true, + 'phpdoc_scalar' => true, + 'phpdoc_separation' => true, + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_tag_type' => [ + 'tags' => [ + 'inheritDoc' => 'inline', + ], + ], + 'phpdoc_to_comment' => [ + 'ignored_tags' => [ + 'var', + ], + ], + 'phpdoc_trim' => true, + 'phpdoc_trim_consecutive_blank_line_separation' => true, + 'phpdoc_types' => true, + 'phpdoc_types_order' => [ + 'null_adjustment' => 'always_first', + 'sort_algorithm' => 'alpha', + ], + 'phpdoc_var_without_name' => true, + 'simple_to_complex_string_variable' => true, + 'simplified_if_return' => true, + 'single_class_element_per_statement' => true, + 'single_import_per_statement' => true, + 'single_line_comment_spacing' => true, + 'single_line_comment_style' => [ + 'comment_types' => [ + 'hash', + ], + ], + 'single_quote' => true, + 'single_space_after_construct' => [ + 'constructs' => [ + 'abstract', + 'as', + 'attribute', + 'break', + 'case', + 'catch', + 'class', + 'clone', + 'comment', + 'const', + 'const_import', + 'continue', + 'do', + 'echo', + 'else', + 'elseif', + 'enum', + 'extends', + 'final', + 'finally', + 'for', + 'foreach', + 'function', + 'function_import', + 'global', + 'goto', + 'if', + 'implements', + 'include', + 'include_once', + 'instanceof', + 'insteadof', + 'interface', + 'match', + 'named_argument', + 'namespace', + 'new', + 'open_tag_with_echo', + 'php_doc', + 'php_open', + 'print', + 'private', + 'protected', + 'public', + 'readonly', + 'require', + 'require_once', + 'return', + 'static', + 'switch', + 'throw', + 'trait', + 'try', + 'type_colon', + 'use', + 'use_lambda', + 'use_trait', + 'var', + 'while', + 'yield', + 'yield_from', + ], + ], + 'space_after_semicolon' => ['remove_in_empty_for_expressions' => true], + 'standardize_increment' => true, + 'standardize_not_equals' => true, + 'switch_continue_to_break' => true, + 'trailing_comma_in_multiline' => [ + 'after_heredoc' => true, + 'elements' => [ + 'arguments', + 'arrays', + 'match', + 'parameters', + ], + ], + 'trim_array_spaces' => true, + 'types_spaces' => true, + 'unary_operator_spaces' => true, + 'whitespace_after_comma_in_array' => true, + 'yoda_style' => [ + 'equal' => false, + 'identical' => false, + 'less_and_greater' => false, + ], + // Risky + '@PSR12:risky' => true, + '@PHP80Migration:risky' => true, + // '@Symfony:risky' => true, // ? revise + 'random_api_migration' => [ + 'replacements' => [ + 'mt_rand' => 'random_int', + 'rand' => 'random_int', + ], + ], + 'declare_strict_types' => false, + 'void_return' => false, // temp, risky + 'modernize_types_casting' => true, + // 'self_accessor' => true, // temp, risky + // 'date_time_immutable' => true, // temp, risky + // 'native_function_invocation' => true, // ping Gorkovoy + // 'native_constant_invocation' => true, // ping Gorkovoy + 'function_to_constant' => true, + 'is_null' => true, +])->setFinder($finder); diff --git a/composer.json b/composer.json index c2339a8..caf68de 100755 --- a/composer.json +++ b/composer.json @@ -16,15 +16,16 @@ } ], "require": { - "php": "^7.2|^8.0", - "illuminate/container": "^6.0|^7.0|^8.0|^9.0", - "illuminate/database": "^6.0|^7.0|^8.0|^9.0", - "illuminate/events": "^6.0|^7.0|^8.0|^9.0", - "illuminate/support": "^6.0|^7.0|^8.0|^9.0" + "php": "^8.1", + "illuminate/container": "^10.0", + "illuminate/database": "^10.0", + "illuminate/events": "^10.0", + "illuminate/support": "^10.0" }, "require-dev": { - "phpunit/phpunit": "^8.3|^9.0|^9.3", - "orchestra/testbench": "^4.0|^5.0|^6.0|dev-remove-nice-to-have-deps" + "phpunit/phpunit": "^9.6.0 || ^10.0.7", + "orchestra/testbench": "^8.0", + "friendsofphp/php-cs-fixer": "^3.14" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a2491b7..af6cdb0 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,13 @@ - - - - ./tests/ - - - - - ./src - - + + + + ./src + + + + + ./tests/ + + diff --git a/src/BelongsTo.php b/src/BelongsTo.php index 1df4f29..dca4849 100644 --- a/src/BelongsTo.php +++ b/src/BelongsTo.php @@ -9,7 +9,6 @@ /** * Class BelongsTo. * - * * @property-read \Chelout\RelationshipEvents\Concerns\HasBelongsToEvents $parent */ class BelongsTo extends BelongsToBase implements EventDispatcher @@ -49,7 +48,7 @@ public function dissociate() $result = parent::dissociate(); - if (! is_null($parent)) { + if ($parent !== null) { $this->parent->fireModelBelongsToEvent('dissociated', $this->relationName, $parent); } @@ -59,8 +58,6 @@ public function dissociate() /** * Update the parent model on the relationship. * - * @param array $attributes - * * @return mixed */ public function update(array $attributes) diff --git a/src/BelongsToMany.php b/src/BelongsToMany.php index 425e5dd..61c8b3a 100644 --- a/src/BelongsToMany.php +++ b/src/BelongsToMany.php @@ -9,7 +9,6 @@ /** * Class BelongsToMany. * - * * @property-read \Chelout\RelationshipEvents\Concerns\HasBelongsToManyEvents $parent */ class BelongsToMany extends BelongsToManyBase implements EventDispatcher @@ -23,8 +22,8 @@ class BelongsToMany extends BelongsToManyBase implements EventDispatcher * * Each existing model is detached, and non existing ones are attached. * - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|int|string $ids - * @param bool $touch + * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|int|string $ids + * @param bool $touch * * @return array */ @@ -42,8 +41,8 @@ public function toggle($ids, $touch = true) /** * Sync the intermediate tables with a list of IDs or collection of models. * - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|int|string $ids - * @param bool $detaching + * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|int|string $ids + * @param bool $detaching * * @return array */ @@ -62,8 +61,7 @@ public function sync($ids, $detaching = true) * Update an existing pivot record on the table. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch * * @return int */ @@ -82,8 +80,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true) * Attach a model to the parent. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch */ public function attach($id, array $attributes = [], $touch = true) { @@ -98,14 +95,14 @@ public function attach($id, array $attributes = [], $touch = true) * Detach models from the relationship. * * @param mixed $ids - * @param bool $touch + * @param bool $touch * * @return int */ public function detach($ids = null, $touch = true) { // Get detached ids to pass them to event - $ids = $ids ?? $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); + $ids ??= $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); $this->parent->fireModelBelongsToManyEvent('detaching', $this->getRelationName(), $ids); diff --git a/src/Concerns/HasBelongsToEvents.php b/src/Concerns/HasBelongsToEvents.php index 6d728ea..e6b8a3f 100644 --- a/src/Concerns/HasBelongsToEvents.php +++ b/src/Concerns/HasBelongsToEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\BelongsTo; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasBelongsToEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasBelongsToEvents @@ -17,11 +17,9 @@ trait HasBelongsToEvents /** * Instantiate a new BelongsTo relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $child - * @param string $foreignKey - * @param string $ownerKey - * @param string $relation + * @param string $foreignKey + * @param string $ownerKey + * @param string $relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ @@ -33,8 +31,8 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelBelongsToEvent($event, $callback) { @@ -48,7 +46,7 @@ protected static function registerModelBelongsToEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToAssociating($callback) { @@ -58,7 +56,7 @@ public static function belongsToAssociating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToAssociated($callback) { @@ -68,7 +66,7 @@ public static function belongsToAssociated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToDissociating($callback) { @@ -78,7 +76,7 @@ public static function belongsToDissociating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToDissociated($callback) { @@ -88,7 +86,7 @@ public static function belongsToDissociated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToUpdating($callback) { @@ -98,7 +96,7 @@ public static function belongsToUpdating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToUpdated($callback) { @@ -108,16 +106,16 @@ public static function belongsToUpdated($callback) /** * Fire the given event for the model relationship. * - * @param string $event - * @param string $relation + * @param string $event + * @param string $relation * @param \Illuminate\Database\Eloquent\Model|int|string $parent - * @param bool $halt + * @param bool $halt * * @return bool */ public function fireModelBelongsToEvent($event, $relation, $parent, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -129,15 +127,16 @@ public function fireModelBelongsToEvent($event, $relation, $parent, $halt = true $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $relation, $parent) + $this->fireCustomModelEvent($event, $method, $relation, $parent), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $relation, $this, $parent, diff --git a/src/Concerns/HasBelongsToManyEvents.php b/src/Concerns/HasBelongsToManyEvents.php index 2779c58..e1af1ab 100644 --- a/src/Concerns/HasBelongsToManyEvents.php +++ b/src/Concerns/HasBelongsToManyEvents.php @@ -4,13 +4,13 @@ use Chelout\RelationshipEvents\BelongsToMany; use Chelout\RelationshipEvents\Helpers\AttributesMethods; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasBelongsToManyEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasBelongsToManyEvents @@ -18,28 +18,33 @@ trait HasBelongsToManyEvents /** * Instantiate a new BelongsToMany relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $table - * @param string $foreignPivotKey - * @param string $relatedPivotKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relationName + * @param string $table + * @param string $foreignPivotKey + * @param string $relatedPivotKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relationName * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ - protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, - $parentKey, $relatedKey, $relationName = null) - { + protected function newBelongsToMany( + Builder $query, + Model $parent, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null, + ) { return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); } /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelBelongsToManyEvent($event, $callback) { @@ -53,7 +58,7 @@ protected static function registerModelBelongsToManyEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyAttaching($callback) { @@ -63,7 +68,7 @@ public static function belongsToManyAttaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyAttached($callback) { @@ -73,7 +78,7 @@ public static function belongsToManyAttached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyDetaching($callback) { @@ -83,7 +88,7 @@ public static function belongsToManyDetaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyDetached($callback) { @@ -93,7 +98,7 @@ public static function belongsToManyDetached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManySyncing($callback) { @@ -103,7 +108,7 @@ public static function belongsToManySyncing($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManySynced($callback) { @@ -113,7 +118,7 @@ public static function belongsToManySynced($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyToggling($callback) { @@ -123,7 +128,7 @@ public static function belongsToManyToggling($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyToggled($callback) { @@ -133,7 +138,7 @@ public static function belongsToManyToggled($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyUpdatingExistingPivot($callback) { @@ -143,7 +148,7 @@ public static function belongsToManyUpdatingExistingPivot($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function belongsToManyUpdatedExistingPivot($callback) { @@ -155,15 +160,15 @@ public static function belongsToManyUpdatedExistingPivot($callback) * * @param string $event * @param string $relation - * @param mixed $ids - * @param array $attributes - * @param bool $halt + * @param mixed $ids + * @param array $attributes + * @param bool $halt * * @return mixed */ public function fireModelBelongsToManyEvent($event, $relation, $ids, $attributes = [], $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -179,15 +184,16 @@ public function fireModelBelongsToManyEvent($event, $relation, $ids, $attributes $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent) + $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $relation, $this, $parsedIdsForEvent, diff --git a/src/Concerns/HasManyEvents.php b/src/Concerns/HasManyEvents.php index dc20e8e..394b9e8 100644 --- a/src/Concerns/HasManyEvents.php +++ b/src/Concerns/HasManyEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\HasMany; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasManyEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasManyEvents @@ -17,10 +17,8 @@ trait HasManyEvents /** * Instantiate a new HasMany relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $foreignKey - * @param string $localKey + * @param string $foreignKey + * @param string $localKey * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ @@ -32,8 +30,8 @@ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $local /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelHasManyEvent($event, $callback) { @@ -47,7 +45,7 @@ protected static function registerModelHasManyEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManyCreating($callback) { @@ -57,7 +55,7 @@ public static function hasManyCreating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManyCreated($callback) { @@ -67,7 +65,7 @@ public static function hasManyCreated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManySaving($callback) { @@ -77,7 +75,7 @@ public static function hasManySaving($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManySaved($callback) { @@ -87,7 +85,7 @@ public static function hasManySaved($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManyUpdating($callback) { @@ -97,7 +95,7 @@ public static function hasManyUpdating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasManyUpdated($callback) { @@ -108,14 +106,14 @@ public static function hasManyUpdated($callback) * Fire the given event for the model relationship. * * @param string $event - * @param mixed $related - * @param bool $halt + * @param mixed $related + * @param bool $halt * * @return mixed */ public function fireModelHasManyEvent($event, $related = null, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -127,15 +125,16 @@ public function fireModelHasManyEvent($event, $related = null, $halt = true) $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $related) + $this->fireCustomModelEvent($event, $method, $related), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $this, $related, ] diff --git a/src/Concerns/HasMorphManyEvents.php b/src/Concerns/HasMorphManyEvents.php index 551baeb..63d1eb2 100644 --- a/src/Concerns/HasMorphManyEvents.php +++ b/src/Concerns/HasMorphManyEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\MorphMany; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasMorphManyEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasMorphManyEvents @@ -17,11 +17,9 @@ trait HasMorphManyEvents /** * Instantiate a new MorphMany relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $type - * @param string $id - * @param string $localKey + * @param string $type + * @param string $id + * @param string $localKey * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ @@ -33,8 +31,8 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelMorphManyEvent($event, $callback) { @@ -48,7 +46,7 @@ protected static function registerModelMorphManyEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManyCreating($callback) { @@ -58,7 +56,7 @@ public static function morphManyCreating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManyCreated($callback) { @@ -68,7 +66,7 @@ public static function morphManyCreated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManySaving($callback) { @@ -78,7 +76,7 @@ public static function morphManySaving($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManySaved($callback) { @@ -88,7 +86,7 @@ public static function morphManySaved($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManyUpdating($callback) { @@ -98,7 +96,7 @@ public static function morphManyUpdating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphManyUpdated($callback) { @@ -109,14 +107,14 @@ public static function morphManyUpdated($callback) * Fire the given event for the model relationship. * * @param string $event - * @param mixed $related - * @param bool $halt + * @param mixed $related + * @param bool $halt * * @return mixed */ public function fireModelMorphManyEvent($event, $related = null, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -128,15 +126,16 @@ public function fireModelMorphManyEvent($event, $related = null, $halt = true) $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $related) + $this->fireCustomModelEvent($event, $method, $related), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $this, $related, ] diff --git a/src/Concerns/HasMorphOneEvents.php b/src/Concerns/HasMorphOneEvents.php index e0fc3f7..0470ac7 100644 --- a/src/Concerns/HasMorphOneEvents.php +++ b/src/Concerns/HasMorphOneEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\MorphOne; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasMorphOneEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasMorphOneEvents @@ -17,11 +17,9 @@ trait HasMorphOneEvents /** * Instantiate a new MorphOne relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $type - * @param string $id - * @param string $localKey + * @param string $type + * @param string $id + * @param string $localKey * * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ @@ -33,8 +31,8 @@ protected function newMorphOne(Builder $query, Model $parent, $type, $id, $local /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelMorphOneEvent($event, $callback) { @@ -48,7 +46,7 @@ protected static function registerModelMorphOneEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneCreating($callback) { @@ -58,7 +56,7 @@ public static function morphOneCreating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneCreated($callback) { @@ -68,7 +66,7 @@ public static function morphOneCreated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneSaving($callback) { @@ -78,7 +76,7 @@ public static function morphOneSaving($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneSaved($callback) { @@ -88,7 +86,7 @@ public static function morphOneSaved($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneUpdating($callback) { @@ -98,7 +96,7 @@ public static function morphOneUpdating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphOneUpdated($callback) { @@ -109,14 +107,14 @@ public static function morphOneUpdated($callback) * Fire the given event for the model relationship. * * @param string $event - * @param mixed $related - * @param bool $halt + * @param mixed $related + * @param bool $halt * * @return mixed */ public function fireModelMorphOneEvent($event, $related = null, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -128,15 +126,16 @@ public function fireModelMorphOneEvent($event, $related = null, $halt = true) $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $related) + $this->fireCustomModelEvent($event, $method, $related), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $this, $related, ] diff --git a/src/Concerns/HasMorphToEvents.php b/src/Concerns/HasMorphToEvents.php index a37322d..422e599 100644 --- a/src/Concerns/HasMorphToEvents.php +++ b/src/Concerns/HasMorphToEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\MorphTo; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasMorphToEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasMorphToEvents @@ -17,12 +17,10 @@ trait HasMorphToEvents /** * Instantiate a new MorphTo relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $foreignKey - * @param string $ownerKey - * @param string $type - * @param string $relation + * @param string $foreignKey + * @param string $ownerKey + * @param string $type + * @param string $relation * * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ @@ -34,8 +32,8 @@ protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $owner /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelMorphToEvent($event, $callback) { @@ -49,7 +47,7 @@ protected static function registerModelMorphToEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToAssociating($callback) { @@ -59,7 +57,7 @@ public static function morphToAssociating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToAssociated($callback) { @@ -69,7 +67,7 @@ public static function morphToAssociated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToDissociating($callback) { @@ -79,7 +77,7 @@ public static function morphToDissociating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToDissociated($callback) { @@ -89,7 +87,7 @@ public static function morphToDissociated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToUpdating($callback) { @@ -99,7 +97,7 @@ public static function morphToUpdating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToUpdated($callback) { @@ -109,16 +107,16 @@ public static function morphToUpdated($callback) /** * Fire the given event for the model relationship. * - * @param string $event - * @param string $relation + * @param string $event + * @param string $relation * @param \Illuminate\Database\Eloquent\Model|int|string $parent - * @param bool $halt + * @param bool $halt * * @return mixed */ public function fireModelMorphToEvent($event, $relation, $parent, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -130,15 +128,16 @@ public function fireModelMorphToEvent($event, $relation, $parent, $halt = true) $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $relation, $parent) + $this->fireCustomModelEvent($event, $method, $relation, $parent), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $relation, $this, $parent, diff --git a/src/Concerns/HasMorphToManyEvents.php b/src/Concerns/HasMorphToManyEvents.php index c25fa26..e6398f3 100644 --- a/src/Concerns/HasMorphToManyEvents.php +++ b/src/Concerns/HasMorphToManyEvents.php @@ -4,13 +4,13 @@ use Chelout\RelationshipEvents\Helpers\AttributesMethods; use Chelout\RelationshipEvents\MorphToMany; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasMorphToManyEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasMorphToManyEvents @@ -18,32 +18,48 @@ trait HasMorphToManyEvents /** * Instantiate a new HasManyThrough relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $name - * @param string $table - * @param string $foreignPivotKey - * @param string $relatedPivotKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relationName - * @param bool $inverse + * @param string $name + * @param string $table + * @param string $foreignPivotKey + * @param string $relatedPivotKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relationName + * @param bool $inverse * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ - protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, - $relatedPivotKey, $parentKey, $relatedKey, - $relationName = null, $inverse = false) - { - return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, - $relationName, $inverse); + protected function newMorphToMany( + Builder $query, + Model $parent, + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null, + $inverse = false, + ) { + return new MorphToMany( + $query, + $parent, + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName, + $inverse, + ); } /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelMorphToManyEvent($event, $callback) { @@ -57,7 +73,7 @@ protected static function registerModelMorphToManyEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyCreating($callback) { @@ -67,7 +83,7 @@ public static function morphToManyCreating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyCreated($callback) { @@ -77,7 +93,7 @@ public static function morphToManyCreated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManySaving($callback) { @@ -87,7 +103,7 @@ public static function morphToManySaving($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManySaved($callback) { @@ -97,7 +113,7 @@ public static function morphToManySaved($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyAttaching($callback) { @@ -107,7 +123,7 @@ public static function morphToManyAttaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyAttached($callback) { @@ -117,7 +133,7 @@ public static function morphToManyAttached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyDetaching($callback) { @@ -127,7 +143,7 @@ public static function morphToManyDetaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyDetached($callback) { @@ -137,7 +153,7 @@ public static function morphToManyDetached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManySyncing($callback) { @@ -147,7 +163,7 @@ public static function morphToManySyncing($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManySynced($callback) { @@ -157,7 +173,7 @@ public static function morphToManySynced($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyToggling($callback) { @@ -167,7 +183,7 @@ public static function morphToManyToggling($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyToggled($callback) { @@ -177,7 +193,7 @@ public static function morphToManyToggled($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyUpdatingExistingPivot($callback) { @@ -187,7 +203,7 @@ public static function morphToManyUpdatingExistingPivot($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphToManyUpdatedExistingPivot($callback) { @@ -199,15 +215,15 @@ public static function morphToManyUpdatedExistingPivot($callback) * * @param string $event * @param string $relation - * @param mixed $ids - * @param array $attributes - * @param bool $halt + * @param mixed $ids + * @param array $attributes + * @param bool $halt * * @return mixed */ public function fireModelMorphToManyEvent($event, $relation, $ids, $attributes = [], $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -223,15 +239,16 @@ public function fireModelMorphToManyEvent($event, $relation, $ids, $attributes = $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent) + $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $relation, $this, $parsedIdsForEvent, diff --git a/src/Concerns/HasMorphedByManyEvents.php b/src/Concerns/HasMorphedByManyEvents.php index b1844e5..6dcb5b3 100644 --- a/src/Concerns/HasMorphedByManyEvents.php +++ b/src/Concerns/HasMorphedByManyEvents.php @@ -4,13 +4,13 @@ use Chelout\RelationshipEvents\Helpers\AttributesMethods; use Chelout\RelationshipEvents\MorphedByMany; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasMorphedByManyEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasMorphedByManyEvents @@ -18,32 +18,48 @@ trait HasMorphedByManyEvents /** * Instantiate a new HasManyThrough relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $name - * @param string $table - * @param string $foreignPivotKey - * @param string $relatedPivotKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relationName - * @param bool $inverse + * @param string $name + * @param string $table + * @param string $foreignPivotKey + * @param string $relatedPivotKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relationName + * @param bool $inverse * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ - protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, - $relatedPivotKey, $parentKey, $relatedKey, - $relationName = null, $inverse = false) - { - return new MorphedByMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, - $relationName, $inverse); + protected function newMorphToMany( + Builder $query, + Model $parent, + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null, + $inverse = false, + ) { + return new MorphedByMany( + $query, + $parent, + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName, + $inverse, + ); } /** * Register a model event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelMorphedByManyEvent($event, $callback) { @@ -57,7 +73,7 @@ protected static function registerModelMorphedByManyEvent($event, $callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyCreating($callback) { @@ -67,7 +83,7 @@ public static function morphedByManyCreating($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyCreated($callback) { @@ -77,7 +93,7 @@ public static function morphedByManyCreated($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManySaving($callback) { @@ -87,7 +103,7 @@ public static function morphedByManySaving($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManySaved($callback) { @@ -97,7 +113,7 @@ public static function morphedByManySaved($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyAttaching($callback) { @@ -107,7 +123,7 @@ public static function morphedByManyAttaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyAttached($callback) { @@ -117,7 +133,7 @@ public static function morphedByManyAttached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyDetaching($callback) { @@ -127,7 +143,7 @@ public static function morphedByManyDetaching($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyDetached($callback) { @@ -137,7 +153,7 @@ public static function morphedByManyDetached($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManySyncing($callback) { @@ -147,7 +163,7 @@ public static function morphedByManySyncing($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManySynced($callback) { @@ -157,7 +173,7 @@ public static function morphedByManySynced($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyToggling($callback) { @@ -167,7 +183,7 @@ public static function morphedByManyToggling($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyToggled($callback) { @@ -177,7 +193,7 @@ public static function morphedByManyToggled($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyUpdatingExistingPivot($callback) { @@ -187,7 +203,7 @@ public static function morphedByManyUpdatingExistingPivot($callback) /** * Register a deleted model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function morphedByManyUpdatedExistingPivot($callback) { @@ -199,15 +215,15 @@ public static function morphedByManyUpdatedExistingPivot($callback) * * @param string $event * @param string $relation - * @param mixed $ids - * @param array $attributes - * @param bool $halt + * @param mixed $ids + * @param array $attributes + * @param bool $halt * * @return mixed */ public function fireModelMorphedByManyEvent($event, $relation, $ids, $attributes = [], $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -223,15 +239,16 @@ public function fireModelMorphedByManyEvent($event, $relation, $ids, $attributes $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent) + $this->fireCustomModelEvent($event, $method, $relation, $parsedIdsForEvent, $parseAttributesForEvent), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $relation, $this, $parsedIdsForEvent, diff --git a/src/Concerns/HasOneEvents.php b/src/Concerns/HasOneEvents.php index 03432aa..4e296fe 100644 --- a/src/Concerns/HasOneEvents.php +++ b/src/Concerns/HasOneEvents.php @@ -3,13 +3,13 @@ namespace Chelout\RelationshipEvents\Concerns; use Chelout\RelationshipEvents\HasOne; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** * Trait HasOneEvents. * - * * @mixin \Chelout\RelationshipEvents\Traits\HasDispatchableEvents */ trait HasOneEvents @@ -17,10 +17,8 @@ trait HasOneEvents /** * Instantiate a new HasOne relationship. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $foreignKey - * @param string $localKey + * @param string $foreignKey + * @param string $localKey * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ @@ -32,8 +30,8 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK /** * Register a model has one event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param Closure|string $callback */ protected static function registerModelHasOneEvent($event, $callback) { @@ -47,7 +45,7 @@ protected static function registerModelHasOneEvent($event, $callback) /** * Register a creating model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneCreating($callback) { @@ -57,7 +55,7 @@ public static function hasOneCreating($callback) /** * Register a created model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneCreated($callback) { @@ -67,7 +65,7 @@ public static function hasOneCreated($callback) /** * Register a saving model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneSaving($callback) { @@ -77,7 +75,7 @@ public static function hasOneSaving($callback) /** * Register a saved model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneSaved($callback) { @@ -87,7 +85,7 @@ public static function hasOneSaved($callback) /** * Register a updating model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneUpdating($callback) { @@ -97,7 +95,7 @@ public static function hasOneUpdating($callback) /** * Register a updated model event with the dispatcher. * - * @param \Closure|string $callback + * @param Closure|string $callback */ public static function hasOneUpdated($callback) { @@ -108,14 +106,14 @@ public static function hasOneUpdated($callback) * Fire the given event for the model relationship. * * @param string $event - * @param mixed $related - * @param bool $halt + * @param mixed $related + * @param bool $halt * * @return mixed */ public function fireModelHasOneEvent($event, $related = null, $halt = true) { - if (! isset(static::$dispatcher)) { + if (!isset(static::$dispatcher)) { return true; } @@ -127,15 +125,16 @@ public function fireModelHasOneEvent($event, $related = null, $halt = true) $method = $halt ? 'until' : 'dispatch'; $result = $this->filterModelEventResults( - $this->fireCustomModelEvent($event, $method, $related) + $this->fireCustomModelEvent($event, $method, $related), ); - if (false === $result) { + if ($result === false) { return false; } - return ! empty($result) ? $result : static::$dispatcher->{$method}( - "eloquent.{$event}: " . static::class, [ + return !empty($result) ? $result : static::$dispatcher->{$method}( + "eloquent.{$event}: " . static::class, + [ $this, $related, ] diff --git a/src/Contracts/EventDispatcher.php b/src/Contracts/EventDispatcher.php index 48d6c68..3ed1ed9 100644 --- a/src/Contracts/EventDispatcher.php +++ b/src/Contracts/EventDispatcher.php @@ -15,8 +15,6 @@ public static function getEventDispatcher(); /** * Set the event dispatcher instance. - * - * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher */ public static function setEventDispatcher(Dispatcher $dispatcher); diff --git a/src/Helpers/AttributesMethods.php b/src/Helpers/AttributesMethods.php index 5c0ba8b..57b3b37 100644 --- a/src/Helpers/AttributesMethods.php +++ b/src/Helpers/AttributesMethods.php @@ -34,31 +34,19 @@ public static function parseIds($value) /** * Parse ids for event. - * - * @param array $ids - * - * @return array */ public static function parseIdsForEvent(array $ids): array { - return array_map(function ($key, $id) { - return is_array($id) ? $key : $id; - }, array_keys($ids), $ids); + return array_map(fn ($key, $id) => is_array($id) ? $key : $id, array_keys($ids), $ids); } /** * Parse attributes for event. * * @param mixed $rawIds - * @param array $parsedIds - * @param array $attributes - * - * @return array */ public static function parseAttributesForEvent($rawIds, array $parsedIds, array $attributes = []): array { - return is_array($rawIds) ? array_filter($parsedIds, function ($id) { - return is_array($id) && ! empty($id); - }) : $attributes; + return is_array($rawIds) ? array_filter($parsedIds, fn ($id) => is_array($id) && !empty($id)) : $attributes; } } diff --git a/src/MorphMany.php b/src/MorphMany.php index 482b14a..a8b26e0 100644 --- a/src/MorphMany.php +++ b/src/MorphMany.php @@ -19,8 +19,6 @@ class MorphMany extends MorphManyBase implements EventDispatcher /** * Attach a model instance to the parent model. * - * @param \Illuminate\Database\Eloquent\Model $model - * * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) @@ -29,7 +27,7 @@ public function save(Model $model) $result = parent::save($model); - if (false !== $result) { + if ($result !== false) { $this->fireModelRelationshipEvent('saved', $result, false); } diff --git a/src/MorphTo.php b/src/MorphTo.php index e074158..d71c33b 100644 --- a/src/MorphTo.php +++ b/src/MorphTo.php @@ -9,7 +9,6 @@ /** * Class MorphTo. * - * * @property-read \Chelout\RelationshipEvents\Concerns\HasMorphToEvents $parent */ class MorphTo extends MorphToBase implements EventDispatcher @@ -47,7 +46,7 @@ public function dissociate() $result = parent::dissociate(); - if (! is_null($parent)) { + if ($parent !== null) { $this->parent->fireModelMorphToEvent('dissociated', $this->relationName, $parent); } @@ -57,8 +56,6 @@ public function dissociate() /** * Update the parent model on the relationship. * - * @param array $attributes - * * @return mixed */ public function update(array $attributes) diff --git a/src/MorphToMany.php b/src/MorphToMany.php index e3628f8..376a440 100644 --- a/src/MorphToMany.php +++ b/src/MorphToMany.php @@ -9,7 +9,6 @@ /** * Class MorphToMany. * - * * @property-read \Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents $parent */ class MorphToMany extends MorphToManyBase implements EventDispatcher @@ -22,7 +21,7 @@ class MorphToMany extends MorphToManyBase implements EventDispatcher * Each existing model is detached, and non existing ones are attached. * * @param mixed $ids - * @param bool $touch + * @param bool $touch * * @return array */ @@ -40,8 +39,8 @@ public function toggle($ids, $touch = true) /** * Sync the intermediate tables with a list of IDs or collection of models. * - * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids - * @param bool $detaching + * @param array|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $ids + * @param bool $detaching * * @return array */ @@ -60,8 +59,7 @@ public function sync($ids, $detaching = true) * Update an existing pivot record on the table. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch * * @return int */ @@ -80,8 +78,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true) * Attach a model to the parent. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch */ public function attach($id, array $attributes = [], $touch = true) { @@ -96,14 +93,14 @@ public function attach($id, array $attributes = [], $touch = true) * Detach models from the relationship. * * @param mixed $ids - * @param bool $touch + * @param bool $touch * * @return int */ public function detach($ids = null, $touch = true) { // Get detached ids to pass them to event - $ids = $ids ?? $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); + $ids ??= $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); $this->parent->fireModelMorphToManyEvent('detaching', $this->getRelationName(), $ids); diff --git a/src/MorphedByMany.php b/src/MorphedByMany.php index e7303c7..6f6c01c 100644 --- a/src/MorphedByMany.php +++ b/src/MorphedByMany.php @@ -9,7 +9,6 @@ /** * Class MorphedByMany. * - * * @property-read \Chelout\RelationshipEvents\Concerns\HasMorphedByManyEvents $parent */ class MorphedByMany extends MorphToManyBase implements EventDispatcher @@ -22,7 +21,7 @@ class MorphedByMany extends MorphToManyBase implements EventDispatcher * Each existing model is detached, and non existing ones are attached. * * @param mixed $ids - * @param bool $touch + * @param bool $touch * * @return array */ @@ -40,8 +39,8 @@ public function toggle($ids, $touch = true) /** * Sync the intermediate tables with a list of IDs or collection of models. * - * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids - * @param bool $detaching + * @param array|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $ids + * @param bool $detaching * * @return array */ @@ -60,8 +59,7 @@ public function sync($ids, $detaching = true) * Update an existing pivot record on the table. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch * * @return int */ @@ -80,8 +78,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true) * Attach a model to the parent. * * @param mixed $id - * @param array $attributes - * @param bool $touch + * @param bool $touch */ public function attach($id, array $attributes = [], $touch = true) { @@ -96,14 +93,14 @@ public function attach($id, array $attributes = [], $touch = true) * Detach models from the relationship. * * @param mixed $ids - * @param bool $touch + * @param bool $touch * * @return int */ public function detach($ids = null, $touch = true) { // Get detached ids to pass them to event - $ids = $ids ?? $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); + $ids ??= $this->parent->{$this->getRelationName()}->pluck($this->relatedKey); $this->parent->fireModelMorphedByManyEvent('detaching', $this->getRelationName(), $ids); diff --git a/src/Traits/HasDispatchableEvents.php b/src/Traits/HasDispatchableEvents.php index eb8e96d..ec66b5d 100644 --- a/src/Traits/HasDispatchableEvents.php +++ b/src/Traits/HasDispatchableEvents.php @@ -5,7 +5,6 @@ /** * Trait HasDispatchableEvents. * - * * @mixin \Illuminate\Database\Eloquent\Concerns\HasEvents */ trait HasDispatchableEvents @@ -15,19 +14,19 @@ trait HasDispatchableEvents * * @param string $event * @param string $method - * @param array $params + * @param array $params * - * @return mixed|null + * @return null|mixed */ protected function fireCustomModelEvent($event, $method, ...$params) { - if (! isset($this->dispatchesEvents[$event])) { + if (!isset($this->dispatchesEvents[$event])) { return; } $result = static::$dispatcher->$method(new $this->dispatchesEvents[$event]($this, $params)); - if (! is_null($result)) { + if ($result !== null) { return $result; } } diff --git a/src/Traits/HasEventDispatcher.php b/src/Traits/HasEventDispatcher.php index 6c2cfae..16beb59 100644 --- a/src/Traits/HasEventDispatcher.php +++ b/src/Traits/HasEventDispatcher.php @@ -28,8 +28,6 @@ public static function getEventDispatcher() /** * Set the event dispatcher instance. - * - * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher */ public static function setEventDispatcher(Dispatcher $dispatcher) { diff --git a/src/Traits/HasOneOrManyMethods.php b/src/Traits/HasOneOrManyMethods.php index 4f7059f..f3392fc 100644 --- a/src/Traits/HasOneOrManyMethods.php +++ b/src/Traits/HasOneOrManyMethods.php @@ -8,7 +8,6 @@ /** * Trait HasOneOrManyMethods. * - * * @property-read \Illuminate\Database\Eloquent\Model $related */ trait HasOneOrManyMethods @@ -16,8 +15,6 @@ trait HasOneOrManyMethods /** * Create a new instance of the related model. * - * @param array $attributes - * * @return \Illuminate\Database\Eloquent\Model */ public function create(array $attributes = []) @@ -27,7 +24,7 @@ public function create(array $attributes = []) $this->setForeignAttributesForCreate($instance); - if (false !== $instance->save()) { + if ($instance->save() !== false) { $this->fireModelRelationshipEvent('created', $instance, false); } }); @@ -36,9 +33,7 @@ public function create(array $attributes = []) /** * Attach a model instance to the parent model. * - * @param \Illuminate\Database\Eloquent\Model $model - * - * @return \Illuminate\Database\Eloquent\Model|false + * @return false|\Illuminate\Database\Eloquent\Model */ public function save(Model $model) { @@ -46,7 +41,7 @@ public function save(Model $model) $result = parent::save($model); - if (false !== $result) { + if ($result !== false) { $this->fireModelRelationshipEvent('saved', $result, false); } @@ -56,8 +51,6 @@ public function save(Model $model) /** * Perform an update on all the related models. * - * @param array $attributes - * * @return int */ public function update(array $attributes) @@ -86,8 +79,8 @@ public function update(array $attributes) * Fire the given event for the model relationship. * * @param string $event - * @param mixed $related - * @param bool $halt + * @param mixed $related + * @param bool $halt * * @return mixed */ @@ -98,11 +91,6 @@ public function fireModelRelationshipEvent($event, $related = null, $halt = true /** * Updated related model's attributes. - * - * @param \Illuminate\Database\Eloquent\Model $related - * @param array $attributes - * - * @return \Illuminate\Database\Eloquent\Model */ protected function updateRelated(Model $related, array $attributes): Model { diff --git a/src/Traits/HasRelationshipObservables.php b/src/Traits/HasRelationshipObservables.php index 2ce5ead..4bf2397 100644 --- a/src/Traits/HasRelationshipObservables.php +++ b/src/Traits/HasRelationshipObservables.php @@ -9,7 +9,6 @@ /** * Trait HasRelationshipObservables. * - * * @mixin \Illuminate\Database\Eloquent\Concerns\HasEvents */ trait HasRelationshipObservables @@ -27,18 +26,12 @@ trait HasRelationshipObservables public static function bootHasRelationshipObservables() { $methods = collect( - class_uses(static::class) - )->filter(function ($trait) { - return Str::startsWith($trait, 'Chelout\RelationshipEvents\Concerns'); - })->flatMap(function ($trait) { + class_uses(static::class), + )->filter(fn ($trait) => Str::startsWith($trait, 'Chelout\RelationshipEvents\Concerns'))->flatMap(function ($trait) { $trait = new ReflectionClass($trait); $methods = $trait->getMethods(ReflectionMethod::IS_PUBLIC); - return collect($methods)->filter(function (ReflectionMethod $method) { - return $method->isStatic(); - })->map(function ($method) { - return $method->name; - }); + return collect($methods)->filter(fn (ReflectionMethod $method) => $method->isStatic())->map(fn ($method) => $method->name); })->toArray(); static::mergeRelationshipObservables($methods); @@ -47,8 +40,6 @@ class_uses(static::class) /** * Merge relationship observables. * - * @param array $relationshipObservables - * * @return void */ public static function mergeRelationshipObservables(array $relationshipObservables) @@ -70,14 +61,12 @@ public function getObservableEvents() 'deleting', 'deleted', 'forceDeleted', ], static::getRelationshipObservables(), - $this->observables + $this->observables, ); } /** * Get relationship observables. - * - * @return array */ public static function getRelationshipObservables(): array { From f801b90922f8a52335d281f58657c04a5363c989 Mon Sep 17 00:00:00 2001 From: Viacheslav Ostrovskii Date: Wed, 15 Feb 2023 21:44:40 +0200 Subject: [PATCH 2/3] test fix --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7d0f85d..39f10a4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,4 +37,4 @@ jobs: run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --no-suggest - name: Execute tests - run: vendor/bin/phpunit --verbose + run: vendor/bin/phpunit From d2f556cd365ad4783ffab5082234033d8f8b009e Mon Sep 17 00:00:00 2001 From: Viacheslav Ostrovskii Date: Wed, 15 Feb 2023 21:54:30 +0200 Subject: [PATCH 3/3] composer scripts --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index caf68de..3fb0c2c 100755 --- a/composer.json +++ b/composer.json @@ -27,6 +27,10 @@ "orchestra/testbench": "^8.0", "friendsofphp/php-cs-fixer": "^3.14" }, + "scripts": { + "fix": "php-cs-fixer --config=.php-cs-fixer.php --dry-run --allow-risky=yes --verbose fix", + "test": "phpunit tests" + }, "autoload": { "psr-4": { "Chelout\\RelationshipEvents\\": "src/"