Skip to content

Commit 981d387

Browse files
authored
Merge pull request #31 from diogogomeswww/enhancement/minor-perfomance-improvements
Minor performance improvements and some docblocks
2 parents 24c9091 + c0f75b0 commit 981d387

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/Edge.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class Edge extends \phpDocumentor\GraphViz\Edge
1010

1111
protected $toPort = null;
1212

13+
/**
14+
* @param Node $from
15+
* @param Node $to
16+
* @return Edge|\phpDocumentor\GraphViz\Edge
17+
*/
1318
public static function create(Node $from, Node $to) {
1419
return new static($from, $to);
1520
}

src/ModelFinder.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public function getModelsInDirectory(string $directory): Collection
3434
})->map(function ($path) {
3535
return $this->getFullyQualifiedClassNameFromFile($path);
3636
})->filter(function (string $className) {
37-
return !empty($className);
38-
})->filter(function (string $className) {
39-
return is_subclass_of($className, EloquentModel::class);
37+
return !empty($className) && is_subclass_of($className, EloquentModel::class);
4038
});
4139
}
4240

@@ -53,9 +51,9 @@ protected function getFullyQualifiedClassNameFromFile(string $path): string
5351
$statements = $traverser->traverse($statements);
5452

5553
// get the first namespace declaration in the file
56-
$root_statement = collect($statements)->filter(function ($statement) {
54+
$root_statement = collect($statements)->first(function ($statement) {
5755
return $statement instanceof Namespace_;
58-
})->first();
56+
});
5957

6058
if (! $root_statement) {
6159
return '';

src/RelationFinder.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Database\Eloquent\Relations\Relation;
99
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10-
use Illuminate\Database\Eloquent\Relations\MorphToMany;
1110
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
12-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1311

1412
class RelationFinder
1513
{
@@ -18,21 +16,20 @@ class RelationFinder
1816
*
1917
* @param string $model
2018
* @return Collection
19+
* @throws \ReflectionException
2120
*/
2221
public function getModelRelations(string $model)
2322
{
2423
$class = new ReflectionClass($model);
2524

26-
$traitMethods = Collection::make($class->getTraits())->map(function ($trait) {
25+
$traitMethods = Collection::make($class->getTraits())->map(function (ReflectionClass $trait) {
2726
return Collection::make($trait->getMethods(ReflectionMethod::IS_PUBLIC));
2827
})->flatten();
2928

3029
$methods = Collection::make($class->getMethods(ReflectionMethod::IS_PUBLIC))
3130
->merge($traitMethods)
3231
->reject(function (ReflectionMethod $method) use ($model) {
33-
return $method->class !== $model;
34-
})->reject(function (ReflectionMethod $method) use ($model) {
35-
return $method->getNumberOfParameters() > 0;
32+
return $method->class !== $model || $method->getNumberOfParameters() > 0;
3633
});
3734

3835
$relations = Collection::make();
@@ -46,13 +43,22 @@ public function getModelRelations(string $model)
4643
return $relations;
4744
}
4845

46+
/**
47+
* @param string $qualifiedKeyName
48+
* @return mixed
49+
*/
4950
protected function getParentKey(string $qualifiedKeyName)
5051
{
5152
$segments = explode('.', $qualifiedKeyName);
5253

5354
return end($segments);
5455
}
5556

57+
/**
58+
* @param ReflectionMethod $method
59+
* @param string $model
60+
* @return array|null
61+
*/
5662
protected function getRelationshipFromMethodAndModel(ReflectionMethod $method, string $model)
5763
{
5864
try {

0 commit comments

Comments
 (0)