Skip to content

[WIP] Support throwing functions#2928

Closed
Aerendir wants to merge 2 commits intorectorphp:masterfrom
Aerendir:AnnotateThrowables-support-throwing-functions
Closed

[WIP] Support throwing functions#2928
Aerendir wants to merge 2 commits intorectorphp:masterfrom
Aerendir:AnnotateThrowables-support-throwing-functions

Conversation

@Aerendir
Copy link
Copy Markdown
Contributor

@Aerendir Aerendir commented Feb 25, 2020

The goal of this PR is to support functions and methods that use functions that may throw exceptions.

use function \Safe\sprintf;

class AClass {
    public function aMethod()
    {
        return sprintf('This method may throw an exception');
    }
}

PROBLEM

Currently it is not possible to build the full namespace of the thrown class.

@TomasVotruba , any ideas about how to proceed to get the full namespace of the thrown classes?

Relevant code:

In the commit there is the dump in HTML format that may be useful.

@TomasVotruba
Copy link
Copy Markdown
Member

Why not use the same trick as with docblock and method reflection, like the last time?

@Aerendir
Copy link
Copy Markdown
Contributor Author

Because it doesn't work for functions: Nette doesn't support building the full namespace for functions, only for classes.

@Aerendir
Copy link
Copy Markdown
Contributor Author

@TomasVotruba ping

@TomasVotruba
Copy link
Copy Markdown
Member

Ah. In that case, you need to solve the behavior yourself with:

$node->getAttribute(AttributeKey::USE_NODES)

Foreach it and match the propper name

@TomasVotruba
Copy link
Copy Markdown
Member

Here is some inspiration:

private function matchAliasedObjectType(Node $node, ObjectType $objectType): ?AliasedObjectType
{
/** @var Use_[]|null $uses */
$uses = $node->getAttribute(AttributeKey::USE_NODES);
if ($uses === null) {
return null;
}
foreach ($uses as $use) {
foreach ($use->uses as $useUse) {
if ($useUse->alias === null) {
continue;
}
$useName = $useUse->name->toString();
$alias = $useUse->alias->toString();
$fullyQualifiedName = $useUse->name->toString();
// A. is alias in use statement matching this class alias
if ($useUse->alias->toString() === $objectType->getClassName()) {
return new AliasedObjectType($alias, $fullyQualifiedName);
}
// B. is aliased classes matching the class name
if ($useName === $objectType->getClassName()) {
return new AliasedObjectType($alias, $fullyQualifiedName);
}
}
}

Just instead of ObjectType, you can use the short string name

@Aerendir
Copy link
Copy Markdown
Contributor Author

Ok, I will try this way...

Comment thread composer.json Outdated
@Aerendir Aerendir closed this Mar 3, 2020
@Aerendir Aerendir deleted the AnnotateThrowables-support-throwing-functions branch March 3, 2020 12:11
@Aerendir
Copy link
Copy Markdown
Contributor Author

Aerendir commented Mar 3, 2020

Too many errors: I will send a clean PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants