-
Notifications
You must be signed in to change notification settings - Fork 0
Implement readonly modifier for classes #138
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a1cc74
Implement readonly modifier for classes
thekid a57bc10
Inherit readonly on class to promoted constructor parameters
thekid 59691ae
Prevent dynamic members on readonly classes
thekid 72dfff6
Add test for static properties
thekid 06fa65b
Upgrade to release version
thekid 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
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
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
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
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
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,36 @@ | ||
<?php namespace lang\ast\emit; | ||
|
||
use lang\ast\Code; | ||
|
||
/** | ||
* Implements readonly properties by removing the `readonly` modifier from | ||
* the class and inheriting it to all properties (and promoted constructor | ||
* arguments). | ||
* | ||
* @see https://wiki.php.net/rfc/readonly_classes | ||
*/ | ||
trait ReadonlyClasses { | ||
|
||
protected function emitClass($result, $class) { | ||
if (false !== ($p= array_search('readonly', $class->modifiers))) { | ||
unset($class->modifiers[$p]); | ||
|
||
// Inherit | ||
foreach ($class->body as $member) { | ||
if ($member->is('property')) { | ||
$member->modifiers[]= 'readonly'; | ||
} else if ($member->is('method')) { | ||
foreach ($member->signature->parameters as $param) { | ||
$param->promote && $param->promote.= ' readonly'; | ||
} | ||
} | ||
} | ||
|
||
// Prevent dynamic members | ||
$throw= new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);'); | ||
$result->locals= [[], [], [null => [$throw, $throw]]]; | ||
} | ||
|
||
return parent::emitClass($result, $class); | ||
} | ||
} |
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
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.
This proper error handling means any way of raising an exception from within the emitting phase and having it turned into a lang.ast.Error with file and line set.
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.
Maybe these also need to go into the parser - where, instead of returning
lang.ast.Node
instances directly, we'd do something like:...and have two separate ast implementations: checked and unchecked. This would be part of a greater refactoring, thus keeping it for a separate pull request.