Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use reflection to get table name in ModelPropertyExtension #666

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}