Skip to content
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

Let the DocBlock::ofMethod handle an ~optional~ className #3885

Conversation

wgevaert
Copy link
Contributor

@wgevaert wgevaert commented Oct 6, 2019

Fixes issue #3879

@codecov
Copy link

codecov bot commented Oct 6, 2019

Codecov Report

Merging #3885 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #3885   +/-   ##
=========================================
  Coverage     83.66%   83.66%           
  Complexity     3853     3853           
=========================================
  Files           151      151           
  Lines         10195    10195           
=========================================
  Hits           8530     8530           
  Misses         1665     1665
Impacted Files Coverage Δ Complexity Δ
src/Framework/TestBuilder.php 100% <ø> (ø) 26 <0> (ø) ⬇️
src/Util/TestDox/NamePrettifier.php 87.09% <ø> (ø) 53 <0> (ø) ⬇️
src/Util/Test.php 89.16% <ø> (ø) 168 <0> (ø) ⬇️
src/Util/Annotation/Registry.php 100% <100%> (ø) 8 <3> (ø) ⬇️
src/Util/Annotation/DocBlock.php 83.05% <100%> (ø) 75 <1> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14ee84e...649b496. Read the comment docs.

@sebastianbergmann
Copy link
Owner

@Ocramius Can you have a look at this, please? I cannot really put a finger on it, but at first glance this seems more like a workaround than a solution.

Copy link
Contributor

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix seems correct to me, @sebastianbergmann: we are now recording docblocks by hierarchy level in a class hierarchy.

The only problem I see with the patch is that it introduces a change in the test suite, rather than an addition, and that makes it hard to validate it. I suggest reverting test changes, and adding targeted tests instead.

{
public static function asArrayProvider()
public function asArrayProvider()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest not changing these as part of thr patch: write independent/new fixtures instead. Changing this means changing tests that might be affected by the bug and/or fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed them and wrote similar tests. I hope it is clear, would be happy to hear how it could be done better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd generally suggest reverting the old asset changes: they do prevent regressions (like the one that you are fixing - and BTW thanks very much for your efforts!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now kept the old test, but named it testWithVariousIterableStaticDataProviders instead of testWithVariousIterableDataProviders, and then also made tests for when this provider is non-static, of the parent, called from the parent, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class AbstractVariousIterableDataProviderTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is being tested with this added fixture? I suggest adding a test for what this is checking.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved

@@ -111,7 +114,7 @@ public static function ofMethod(\ReflectionMethod $method): self
$method->getEndLine(),
$method->getFileName(),
$method->getName(),
$method->getDeclaringClass()->getName()
$className ?? $method->getDeclaringClass()->getName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If fetching the specific class is the use-case, then I suggest making the new argument mandatory, and naming $className so that it reflects the fact that this is a specific class in the hierarchy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, it's not optional.

@@ -62,7 +62,7 @@ public function forClassName(string $class): DocBlock

/**
* @throws Exception
* @psalm-param class-string $className
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ow, not sure how this made it through 😱

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with this psalm-param? I do not really understand it, I copied it to more locations, seemed logical to me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a restriction on the string type, stating that the string must be a class name. The @psalm-param is an annotation we have to use here, in lack of better type support in phpdocumentor/phpstorm/etc.

All good BTW 👍

@@ -80,6 +80,6 @@ public function forMethod(string $class, string $method): DocBlock
);
}

return $this->methodDocBlocks[$class][$method] = DocBlock::ofMethod($reflection);
return $this->methodDocBlocks[$class][$method] = DocBlock::ofMethod($reflection, $class);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$class should probably be named $classInHierarchy or such

/**
* @psalm-param class-string $className
*/
public static function ofMethod(\ReflectionMethod $method, string $className = null): self
Copy link
Contributor

@gleb-svitelskiy gleb-svitelskiy Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there are two ways to create DocBlock object

DocBlock::ofClass(
    new \ReflectionClass($class)
);
DocBlock::ofMethod(
    new \ReflectionMethod($class, $method)
);

Unfortunately, you can not obtain class namespace from method reflection object, only class that declares method.
Maybe it's better to describe only one method to create a DocBlock object e.g.

public static function fromReflection(\ReflectionClass $class, \ReflectionMethod $method = null): self

or make a public constructor

public function __construct(\ReflectionClass $class, \ReflectionMethod $method = null)

@Ocramius what do you think about that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about separate named ctors (as separate static methods)?

Also, please keep the ctor private: that's what gave us this flexibility in first place 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a difference between docblocks of classes and those of methods right? I think the current structure makes that quite clear?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, all good with that

@wgevaert
Copy link
Contributor Author

wgevaert commented Oct 7, 2019

Currently having problems with the following:
Adding the class-string to the classNames for psalm gave problems in Framework/DataProviderTestSuite, in the following snippet:

         [$className, $methodName] = \explode('::', $this->getName());

        return TestUtil::getSize($className, $methodName);

So I made this

         [$className, $methodName] = \explode('::', $this->getName());
 
+        /** @psalm-suppress ArgumentTypeCoercion */
         return TestUtil::getSize($className, $methodName);

Now php-Cs-fixer wants it to look like

         [$className, $methodName] = \explode('::', $this->getName());
 
-        /** @psalm-suppress ArgumentTypeCoercion */
+        /* @psalm-suppress ArgumentTypeCoercion */
         return TestUtil::getSize($className, $methodName);

but then psalm does not recognize the psalm-annotation. What should I do?

@Ocramius
Copy link
Contributor

Ocramius commented Oct 7, 2019

If suppressing won't work, then I suggest updating the baseline file. See psalm --help | grep baseline.

If all is good, we should see that one error added to the file, and if lucky even some error removals in the diff 👍

@wgevaert
Copy link
Contributor Author

wgevaert commented Oct 7, 2019

Hurray!

@wgevaert wgevaert changed the title Let the DocBlock::ofMethod handle an optional className Let the DocBlock::ofMethod handle an ~optional~ className Oct 7, 2019
@sebastianbergmann
Copy link
Owner

Cherry-picked into 8.4 and merged to master from there. Thank you!

@wgevaert
Copy link
Contributor Author

wgevaert commented Oct 7, 2019

Did you cherry-pick the whole pull-request or only the first commit? I did make quite some adjustments, thanks to ocramius' feedback, that should certainly be included as well

Edit: Ah, of course you did, I'm sorry for the bump

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.

4 participants