Skip to content

Commit

Permalink
Merge pull request #165 from GeoSot/feat/gs/add-cs-fixer
Browse files Browse the repository at this point in the history
feat: add cs fixer
  • Loading branch information
danharrin authored Oct 2, 2024
2 parents a5ee29c + 257ddee commit 013c4fe
Show file tree
Hide file tree
Showing 32 changed files with 186 additions and 156 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Linting

on:
push:
pull_request:

jobs:
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.php --diff --dry-run
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ vendor
coverage
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
.envrc
33 changes: 33 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

$rules = [
'@Symfony' => true,
'strict_param' => true,
'php_unit_method_casing' => ['case' => 'snake_case'],
'phpdoc_align' => [
'align' => 'left'
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true
],
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
];

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules($rules)
->setFinder($finder)
->setRiskyAllowed(true)
->setUsingCache(true);
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"illuminate/database": "^10.0|^11.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "dev-master",
"laravel/legacy-factories": "^1.0@dev",
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.0"
Expand All @@ -39,7 +40,8 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"lint": "vendor/bin/php-cs-fixer fix -vvv --show-progress=dots --config=.php-cs-fixer.php"
},
"config": {
"sort-packages": true
Expand Down
1 change: 0 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
* You can place your custom package configuration in here.
*/
return [
//
];
8 changes: 4 additions & 4 deletions src/EloquentJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class EloquentJoins
*/
public static function registerEloquentMacros()
{
EloquentQueryBuilder::mixin(new JoinRelationship);
EloquentQueryBuilder::mixin(new QueryRelationshipExistence);
QueryBuilder::mixin(new QueryBuilderExtraMethods);
EloquentQueryBuilder::mixin(new JoinRelationship());
EloquentQueryBuilder::mixin(new QueryRelationshipExistence());
QueryBuilder::mixin(new QueryBuilderExtraMethods());

Relation::mixin(new RelationshipsExtraMethods);
Relation::mixin(new RelationshipsExtraMethods());
}
}
12 changes: 2 additions & 10 deletions src/JoinsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

class JoinsHelper
{
static array $instances = [];
public static array $instances = [];

protected function __construct()
{

}

public static function make(): static
Expand All @@ -24,8 +23,6 @@ public static function make(): static

/**
* Cache to not join the same relationship twice.
*
* @var array
*/
private array $joinRelationshipCache = [];

Expand All @@ -38,12 +35,8 @@ public static function make(): static
'rightJoin' => 'rightPowerJoin',
];


/**
* Format the join callback.
*
* @param mixed $callback
* @return mixed
*/
public function formatJoinCallback($callback)
{
Expand Down Expand Up @@ -71,7 +64,7 @@ public function generateAliasForRelationship(Relation $relation, string $relatio
/**
* Get the join alias name from all the different options.
*/
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): null|string|array
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): string|array|null
{
if ($callback) {
if (is_callable($callback)) {
Expand All @@ -98,7 +91,6 @@ public function getAliasName(bool $useAlias, Relation $relation, string $relatio
: null;
}


/**
* Checks if the relationship was already joined.
*/
Expand Down
24 changes: 14 additions & 10 deletions src/Mixins/JoinRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

/**
* @mixin Builder
*
* @method \Illuminate\Database\Eloquent\Model getModel()
* @property \Illuminate\Database\Eloquent\Builder $query
*
* @property Builder $query
*/
class JoinRelationship
{
Expand Down Expand Up @@ -79,7 +81,7 @@ public function rightPowerJoin(): Closure

public function newPowerJoinClause(): Closure
{
return function (QueryBuilder $parentQuery, $type, $table, Model $model = null) {
return function (QueryBuilder $parentQuery, $type, $table, ?Model $model = null) {
return new PowerJoinClause($parentQuery, $type, $table, $model);
};
}
Expand All @@ -95,7 +97,7 @@ public function joinRelationship(): Closure
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false,
string $morphable = null
?string $morphable = null,
) {
$joinType = JoinsHelper::$joinMethodsMap[$joinType] ?? $joinType;
$useAlias = is_string($callback) ? false : $useAlias;
Expand Down Expand Up @@ -210,7 +212,7 @@ public function joinRelation(): Closure
$callback = null,
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false
bool $disableExtraConditions = false,
) {
return $this->joinRelationship($relationName, $callback, $joinType, $useAlias, $disableExtraConditions);
};
Expand Down Expand Up @@ -255,7 +257,7 @@ public function joinNestedRelationship(): Closure
$joinType = 'join',
$useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null
?string $morphable = null,
) {
$relations = explode('.', $relationships);
$joinHelper = JoinsHelper::make($this->getModel());
Expand All @@ -265,7 +267,7 @@ public function joinNestedRelationship(): Closure
$part = [];
foreach ($relations as $relationName) {
$part[] = $relationName;
$fullRelationName = join(".", $part);
$fullRelationName = join('.', $part);

$currentModel = $latestRelation ? $latestRelation->getModel() : $this->getModel();
$relation = $currentModel->{$relationName}();
Expand Down Expand Up @@ -316,7 +318,6 @@ public function joinNestedRelationship(): Closure
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}


if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
$latestRelation = $relation;

Expand All @@ -337,6 +338,7 @@ public function joinNestedRelationship(): Closure
}

StaticCache::clear();

return $this;
};
}
Expand Down Expand Up @@ -412,6 +414,7 @@ public function orderByPowerJoins(): Closure
);
}
}

return $this;
};
}
Expand Down Expand Up @@ -513,7 +516,7 @@ public function orderByLeftPowerJoinsMax(): Closure
*/
public function powerJoinHas(): Closure
{
return function ($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null, string $morphable = null): static {
return function ($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure|array|null $callback = null, ?string $morphable = null): static {
if (is_null($this->getSelect())) {
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
}
Expand Down Expand Up @@ -541,7 +544,7 @@ public function powerJoinHas(): Closure

public function hasNestedUsingJoins(): Closure
{
return function ($relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array $callback = null): static {
return function ($relations, $operator = '>=', $count = 1, $boolean = 'and', Closure|array|null $callback = null): static {
$relations = explode('.', $relations);

/** @var Relation */
Expand All @@ -564,13 +567,14 @@ public function hasNestedUsingJoins(): Closure

$latestRelation = $relation;
}

return $this;
};
}

public function powerJoinDoesntHave(): Closure
{
return function ($relation, $boolean = 'and', Closure $callback = null) {
return function ($relation, $boolean = 'and', ?Closure $callback = null) {
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
};
}
Expand Down
Loading

0 comments on commit 013c4fe

Please sign in to comment.