-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add #[NonpublicConstructor] attribute #17846
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
Open
DanielEScherzer
wants to merge
4
commits into
php:master
Choose a base branch
from
DanielEScherzer:NonpublicConstructor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: works as a replacement for get_constructor | ||
--FILE-- | ||
<?php | ||
|
||
new Generator(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Call to private Generator::__construct() from global scope, the "Generator" class is reserved for internal use and cannot be manually instantiated, in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %d |
20 changes: 20 additions & 0 deletions
20
Zend/tests/attributes/NonpublicConstructor/Reflection_newInstance.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects message from ReflectionClass::newInstance() | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
private function __construct( $unused ) {} | ||
} | ||
|
||
$reflection = new ReflectionClass( Demo::class ); | ||
$reflection->newInstance( true ); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught ReflectionException: Access to non-public constructor of class Demo, use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 %s(%d): ReflectionClass->newInstance(true) | ||
#1 {main} | ||
thrown in %s on line %d |
20 changes: 20 additions & 0 deletions
20
Zend/tests/attributes/NonpublicConstructor/Reflection_newInstanceArgs.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects message from ReflectionClass::newInstanceArgs() | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
private function __construct( $unused ) {} | ||
} | ||
|
||
$reflection = new ReflectionClass( Demo::class ); | ||
$reflection->newInstanceArgs( [ true ] ); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught ReflectionException: Access to non-public constructor of class Demo, use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 %s(%d): ReflectionClass->newInstanceArgs(Array) | ||
#1 {main} | ||
thrown in %s on line %d |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: using the attribute itself as an object | ||
--FILE-- | ||
<?php | ||
|
||
$attrib = new NonpublicConstructor('example'); | ||
var_dump( $attrib ); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(NonpublicConstructor)#%d (1) { | ||
["message"]=> | ||
string(7) "example" | ||
} |
18 changes: 18 additions & 0 deletions
18
Zend/tests/attributes/NonpublicConstructor/global_private.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects error message (private constructor, global scope) | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
private function __construct() {} | ||
} | ||
|
||
new Demo(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Call to private Demo::__construct() from global scope, use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %s |
18 changes: 18 additions & 0 deletions
18
Zend/tests/attributes/NonpublicConstructor/global_protected.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects error message (protected constructor, global scope) | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
protected function __construct() {} | ||
} | ||
|
||
new Demo(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Call to protected Demo::__construct() from global scope, use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %s |
13 changes: 13 additions & 0 deletions
13
Zend/tests/attributes/NonpublicConstructor/must_be_constructor.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: On a private non-constructor | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor] | ||
private function test() {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: #[NonpublicConstructor] can only be applied to constructors in %s on line %d |
13 changes: 13 additions & 0 deletions
13
Zend/tests/attributes/NonpublicConstructor/must_be_nonpublic.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: On a public constructor | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor] | ||
public function __construct() {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: #[NonpublicConstructor] can only be applied to protected or private constructors in %s on line %d |
13 changes: 13 additions & 0 deletions
13
Zend/tests/attributes/NonpublicConstructor/non_string.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: Message argument must be a string | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor(true)] | ||
private function __construct() {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: NonpublicConstructor::__construct(): Argument #1 ($message) must be of type string, true given in %s on line %d |
25 changes: 25 additions & 0 deletions
25
Zend/tests/attributes/NonpublicConstructor/subclass_private.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects error message (private constructor, protected scope) | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
private function __construct() {} | ||
} | ||
|
||
class Subclass extends Demo { | ||
public static function create() { | ||
return new Demo(); | ||
} | ||
} | ||
|
||
Subclass::create(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Call to private Demo::__construct() from scope Subclass, use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 %s(%d): Subclass::create() | ||
#1 {main} | ||
thrown in %s on line %s |
25 changes: 25 additions & 0 deletions
25
Zend/tests/attributes/NonpublicConstructor/subclass_without_new.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: affects error message (private constructor, protected scope, called via `parent::__construct()`) | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor("use ::getInstance() instead")] | ||
private function __construct() {} | ||
} | ||
|
||
class Subclass extends Demo { | ||
public function __construct() { | ||
parent::__construct(); | ||
} | ||
} | ||
|
||
new Subclass(); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Error: Cannot call private Demo::__construct(), use ::getInstance() instead, in %s:%d | ||
Stack trace: | ||
#0 %s(%d): Subclass->__construct() | ||
#1 {main} | ||
thrown in %s on line %s |
13 changes: 13 additions & 0 deletions
13
Zend/tests/attributes/NonpublicConstructor/without_message.phpt
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
#[\NonpublicConstructor]: Message argument is required | ||
--FILE-- | ||
<?php | ||
|
||
class Demo { | ||
#[\NonpublicConstructor] | ||
private function __construct() {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: #[NonpublicConstructor] takes 1 parameter, 0 given in %s on line %d |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test for this path would be nice to have