Skip to content

Commit

Permalink
Merge pull request #666 from nunomaduro/patch-1
Browse files Browse the repository at this point in the history
fix: use reflection to get table name in ModelPropertyExtension
  • Loading branch information
canvural committed Sep 25, 2020
2 parents 32ab011 + 127febd commit 64457d5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\IntegerType;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand Down Expand Up @@ -73,9 +74,17 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
}

$modelName = $classReflection->getNativeReflection()->getName();
/** @var Model $modelInstance */
$modelInstance = new $modelName;
$tableName = $modelInstance->getTable();

try {
$reflect = new \ReflectionClass($modelName);

/** @var Model $modelInstance */
$modelInstance = $reflect->newInstanceWithoutConstructor();

$tableName = $modelInstance->getTable();
} catch (\ReflectionException $e) {
return false;
}

if (! array_key_exists($tableName, $this->tables)) {
return false;
Expand Down Expand Up @@ -105,9 +114,18 @@ public function getProperty(
): PropertyReflection {
$modelName = $classReflection->getNativeReflection()->getName();

/** @var Model $modelInstance */
$modelInstance = new $modelName;
$tableName = $modelInstance->getTable();
try {
$reflect = new \ReflectionClass($modelName);

/** @var Model $modelInstance */
$modelInstance = $reflect->newInstanceWithoutConstructor();

$tableName = $modelInstance->getTable();
} catch (\ReflectionException $e) {
// `hasProperty` should return false if there was a reflection exception.
// so this should never happen
throw new ShouldNotHappenException();
}

if (
(! array_key_exists($tableName, $this->tables)
Expand Down
23 changes: 23 additions & 0 deletions tests/Application/app/GuardedModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App;

use Illuminate\Database\Eloquent\Model;

class GuardedModel extends Model
{
protected $guarded = ['text'];

/**
* @param array<string, mixed> $attributes
*/
public function __construct(array $attributes = [])
{
dd(debug_backtrace()[0]);
$attributes['name'] = $attributes['name'] ?? 'foo';

parent::__construct($attributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGuardedModelsTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('guarded_models', static function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('text');
$table->timestamps();
});
}
}
6 changes: 6 additions & 0 deletions tests/Features/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Account;
use App\Group;
use App\GuardedModel;
use App\Role;
use App\User;
use Carbon\Carbon as BaseCarbon;
Expand Down Expand Up @@ -110,4 +111,9 @@ public function testWriteIdPropertyWhenMigrationsCouldntBeRead(): bool

return $group->save();
}

public function testModelWithGuardedProperties(GuardedModel $guardedModel): string
{
return $guardedModel->name;
}
}

0 comments on commit 64457d5

Please sign in to comment.