-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: deprecate stuff in Instantiator
- Loading branch information
Showing
10 changed files
with
108 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @method static self withoutConstructor() | ||
* @method self withoutConstructor() | ||
*/ | ||
final class Instantiator | ||
{ | ||
|
@@ -35,6 +38,13 @@ final class Instantiator | |
/** @var string[] */ | ||
private array $forceProperties = []; | ||
|
||
public function __construct(bool $calledInternally = false) | ||
{ | ||
if (!$calledInternally) { | ||
trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('%1$s constructor will be private in Foundry 2.0. Use either "%1$s::withConstructor()" or "%1$s::withoutConstructor()"', self::class)); | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
|
@@ -90,16 +100,37 @@ public function __invoke(array $attributes, string $class): object | |
return $object; | ||
} | ||
|
||
/** | ||
* Instantiate objects without calling the constructor. | ||
*/ | ||
public function withoutConstructor(): self | ||
public function __call(string $name, array $arguments): self | ||
{ | ||
if ('withoutConstructor' !== $name) { | ||
throw new \BadMethodCallException(\sprintf('Call to undefined method "%s::%s".', static::class, $name)); | ||
} | ||
|
||
trigger_deprecation('zenstruck/foundry', '1.37.0', 'Calling instance method "%1$s::withoutConstructor()" is deprecated and will be removed in 2.0. Use static call instead: "%1$s::withoutConstructor()" instead.', static::class); | ||
|
||
$this->withoutConstructor = true; | ||
|
||
return $this; | ||
} | ||
|
||
public static function __callStatic(string $name, array $arguments): self | ||
{ | ||
if ('withoutConstructor' !== $name) { | ||
throw new \BadMethodCallException(\sprintf('Call to undefined method "%s::%s".', static::class, $name)); | ||
} | ||
|
||
$instance = new self(calledInternally: true); | ||
|
||
$instance->withoutConstructor = true; | ||
|
||
return $instance; | ||
} | ||
|
||
public static function withConstructor(): self | ||
{ | ||
return new self(calledInternally: true); | ||
} | ||
|
||
/** | ||
* Ignore attributes that can't be set to object. | ||
* | ||
|
@@ -240,6 +271,10 @@ private function instantiate(string $class, array &$attributes): object | |
$constructor = $class->getConstructor(); | ||
|
||
if ($this->withoutConstructor || !$constructor || !$constructor->isPublic()) { | ||
if (!$this->withoutConstructor && $constructor && !$constructor->isPublic()) { | ||
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Instantiator was created to instantiate "%s" by calling the constructor whereas the constructor is not public. This is deprecated and will throw an exception in Foundry 2.0. Use "%s::withoutConstructor()" instead or make constructor public.', $class->getName(), self::class); | ||
} | ||
|
||
return $class->newInstanceWithoutConstructor(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.