Skip to content

Commit

Permalink
fix(MethodClassReflectionExtension): Fix bug with how many_many 'thro…
Browse files Browse the repository at this point in the history
…ugh' logic works - Github Issue #13

- Update documentation to include known limitations
  • Loading branch information
silbinarywolf committed Jun 9, 2018
1 parent 7fadf20 commit d0feb04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ The pattern will be as follows until I no longer need to maintain support for Si
* [Advanced Usage](docs/en/advanced-usage.md)
* [License](LICENSE.md)

## Known Limitations

* The type of the `owner` property can't be reasoned about for extensions. You must use `getOwner()`. Related Issues: [#1043](https://github.com/phpstan/phpstan/issues/1043) and [#1044](https://github.com/phpstan/phpstan/issues/1044)

## Credits

* [Ondřej Mirtes](https://github.com/ondrejmirtes) for his amazing work on the PHPStan library
Expand Down
16 changes: 13 additions & 3 deletions src/Reflection/MethodClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,24 @@ private function createMethods(ClassReflection $classReflection): array

foreach ($componentNameValueMap as $methodName => $type) {
if (is_array($type)) {
if (!isset($type['through'])) {
throw new Exception('Unknown array format. Expected string or array with "through".');
if ($componentType !== 'many_many') {
throw new Exception('Cannot use array format for "'.$componentType.'" component type.');
}
if (!isset($type['through']) ||
!isset($type['from']) ||
!isset($type['to'])) {
throw new Exception('Unknown array format. Expected string or array with "through", "from" and "to".');
}
// Example data:
// array(3) {["through" => "SilverStripe\Assets\Shortcodes\FileLink"]
// ["from" => "Parent"]
// ["to" => "Linked"]
$type = $type['through'];
$toClass = $type['to'];
$throughClass = $type['through'];
$throughClassHasOne = ConfigHelper::get($throughClass, 'has_one');

This comment has been minimized.

Copy link
@tractorcow

tractorcow Jun 10, 2018

Looks good :D

This comment has been minimized.

Copy link
@silbinarywolf

silbinarywolf Jun 11, 2018

Author Contributor

Haha what the hell. You can just arbitrarily comment on commits now? :O

This comment has been minimized.

Copy link
@tractorcow

tractorcow Jun 11, 2018

Since years ago. ;p

if ($throughClassHasOne && isset($throughClassHasOne[$toClass])) {
$type = $throughClassHasOne[$toClass];
}
}
// Ignore parameters
$type = explode('(', $type, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@ public function testLinkTrackingMethod(): void
// This statement is needed so PHPStan knows $resultType is ObjectType.
return;
}
self::assertContains(
self::assertSame(
$resultType->getClassName(),
[
ClassHelper::SiteTree, // Expected value for: SilverStripe 4.0
ClassHelper::SiteTreeLink, // Expected value for: SilverStripe 4.2+ (master branch at time of writing, 4.2+ is an assumption)
]
ClassHelper::SiteTree
);
}
}

0 comments on commit d0feb04

Please sign in to comment.