Skip to content

Commit

Permalink
Seal properties and methods whenever @method/@Property is used
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 3, 2020
1 parent bf310fb commit df33405
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
32 changes: 18 additions & 14 deletions src/Psalm/Internal/Visitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,9 @@ function (array $l, array $r) : int {
$this->implementTemplatedType($storage, $node, $implemented_class_name);
}

$storage->sealed_properties = $docblock_info->sealed_properties;
$storage->sealed_methods = $docblock_info->sealed_methods;

if ($docblock_info->properties) {
foreach ($docblock_info->properties as $property) {
$pseudo_property_type_tokens = Type::fixUpLocalType(
Expand Down Expand Up @@ -1233,15 +1236,27 @@ function (array $l, array $r) : int {
$storage->has_docblock_issues = true;
}
}

$storage->sealed_properties = true;
}

foreach ($docblock_info->methods as $method) {
/** @var MethodStorage */
$pseudo_method_storage = $this->registerFunctionLike($method, true);

if ($pseudo_method_storage->is_static) {
$storage->pseudo_static_methods[strtolower($method->name->name)] = $pseudo_method_storage;
} else {
$storage->pseudo_methods[strtolower($method->name->name)] = $pseudo_method_storage;
}

$storage->sealed_methods = true;
}

$storage->deprecated = $docblock_info->deprecated;
$storage->internal = $docblock_info->internal;
$storage->psalm_internal = $docblock_info->psalm_internal;

$storage->sealed_properties = $docblock_info->sealed_properties;
$storage->sealed_methods = $docblock_info->sealed_methods;

if ($docblock_info->mixin) {
if (isset($this->class_template_types[$docblock_info->mixin])) {
if (IssueBuffer::accepts(
Expand Down Expand Up @@ -1270,17 +1285,6 @@ function (array $l, array $r) : int {
$storage->override_method_visibility = $docblock_info->override_method_visibility;

$storage->suppressed_issues = $docblock_info->suppressed_issues;

foreach ($docblock_info->methods as $method) {
/** @var MethodStorage */
$pseudo_method_storage = $this->registerFunctionLike($method, true);

if ($pseudo_method_storage->is_static) {
$storage->pseudo_static_methods[strtolower($method->name->name)] = $pseudo_method_storage;
} else {
$storage->pseudo_methods[strtolower($method->name->name)] = $pseudo_method_storage;
}
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/MagicMethodAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ class Child extends ParentClass {}
$b = $child->setString(5);
$c = $child->getBool("hello");
$d = $child->getArray();
$child->setArray(["boo"]);
$e = $child->getCallable();
$child->setMixed("hello");
$child->setMixed(4);
Expand Down Expand Up @@ -586,7 +585,6 @@ public function __call(string $name, array $args) {}
/**
* @method string getString()
* @psalm-seal-methods
*/
class Child extends ParentClass {}
Expand Down
9 changes: 3 additions & 6 deletions tests/MagicPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function __set($name, $value): void {
}
$a = new A();
$a->foo = "hello";
$a->bar = "hello"; // not a property',
$a->foo = "hello";',
],
'propertyOfTypeClassDocblock' => [
'<?php
Expand Down Expand Up @@ -449,8 +448,7 @@ public function __set($name, $value): void {
}
$a = new A();
$a->foo = "hello";
$a->bar = "hello"; // not a property',
$a->foo = "hello";',
],
'overridePropertyAnnotations' => [
'<?php
Expand Down Expand Up @@ -479,8 +477,7 @@ public function __set($name, $value): void {
}
$a = new A();
$a->foo = "hello";
$a->bar = "hello"; // not a property',
$a->foo = "hello";',
],
'overrideWithReadWritePropertyAnnotations' => [
'<?php
Expand Down

0 comments on commit df33405

Please sign in to comment.