Skip to content

2.x coauthors fixes#2157

Merged
jarednova merged 20 commits into2.x-factoriesfrom
2.x-coauthors-fixes
Apr 14, 2020
Merged

2.x coauthors fixes#2157
jarednova merged 20 commits into2.x-factoriesfrom
2.x-coauthors-fixes

Conversation

@acobster
Copy link
Copy Markdown
Collaborator

@acobster acobster commented Jan 8, 2020

Ticket: #2155

Issue

The Co-Authors Plus integration as it currently exists is incompatible with Factories and Class Maps. This is an attempt to make them compatible.

Solution

I describe the proposed solution in more detail in the issue write-up, but basically what I'm doing here is treating "guest authors" as a special case of the timber/user/classmap filter: instead of calling a Factory, it applies the filter directly.

Impact

Should be none.

Usage Changes

None, other than adding support for class maps in this integration.

Considerations

I may be wrong about everything. :D

Testing

Updated tests where it made sense and added a test specifically for dealing with overriding the class instantiated for guest authors.

Coby Tamayo added 2 commits January 7, 2020 19:03
"Guest Authors" as returned from get_coauthors() are just stdclass objects,
which are not supported in UserFactory. This adds support at the
integration level to instantiate these as CoAuthorsPlusUser instances
directly.
@acobster acobster changed the base branch from 2.x-user-factories to 2.x-factories January 11, 2020 00:28
@acobster
Copy link
Copy Markdown
Collaborator Author

Detaching this from 2.x-user-factories for now. We can revisit later.

Doing this mainly for simplicity's sake, since the status of this integration is up in the air.
Further discussion to be had on PR #2157.
@acobster acobster force-pushed the 2.x-coauthors-fixes branch from 5fdb5f6 to bcd4d90 Compare January 11, 2020 00:47
acobster pushed a commit that referenced this pull request Jan 11, 2020
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 18, 2020

Codecov Report

❗ No coverage uploaded for pull request base (2.x-factories@2dc9794). Click here to learn what that means.
The diff coverage is 88.6%.

Impacted file tree graph

@@               Coverage Diff               @@
##             2.x-factories   #2157   +/-   ##
===============================================
  Coverage                 ?   93.7%           
  Complexity               ?    1610           
===============================================
  Files                    ?      54           
  Lines                    ?    4069           
  Branches                 ?       0           
===============================================
  Hits                     ?    3813           
  Misses                   ?     256           
  Partials                 ?       0
Impacted Files Coverage Δ Complexity Δ
lib/Comment.php 91.35% <ø> (ø) 64 <0> (?)
lib/Image.php 96.49% <ø> (ø) 22 <0> (?)
lib/Twig.php 98.87% <ø> (ø) 51 <0> (?)
lib/Site.php 54.28% <ø> (ø) 18 <0> (?)
lib/Timber.php 86.36% <100%> (ø) 39 <0> (?)
lib/MenuItem.php 94.11% <100%> (ø) 38 <0> (?)
lib/Integrations/CoAuthorsPlus.php 100% <100%> (ø) 10 <0> (?)
lib/DateTimeHelper.php 100% <100%> (ø) 13 <0> (?)
lib/Integrations/CoAuthorsPlusUser.php 77.77% <100%> (ø) 5 <0> (?)
lib/Helper.php 93.58% <100%> (ø) 78 <0> (?)
... and 8 more

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 2dc9794...9bdb2f8. Read the comment docs.

@acobster acobster requested a review from jarednova as a code owner February 18, 2020 00:09
@jarednova
Copy link
Copy Markdown
Member

Oy! That wasn't any fun. But I think it's done?!?!?! Let's see how the tests go. There are a couple of things to review when it comes to new static(); in the constructor(s)

@jarednova
Copy link
Copy Markdown
Member

The three static analysis errors are ...

 ------ ------------------------------------------------------- 
  Line   Comment.php                                            
 ------ ------------------------------------------------------- 
  123    Unsafe usage of new static().                          
         💡 Consider making the class or the constructor final.  
 ------ ------------------------------------------------------- 
 ------ ------------------------------------------------------- 
  Line   Integrations/CoAuthorsPlusUser.php                     
 ------ ------------------------------------------------------- 
  14     Unsafe usage of new static().                          
         💡 Consider making the class or the constructor final.  
 ------ ------------------------------------------------------- 
 ------ ------------------------------------------------------- 
  Line   User.php                                               
 ------ ------------------------------------------------------- 
  115    Unsafe usage of new static().                          
         💡 Consider making the class or the constructor final.  
 ------ ------------------------------------------------------- 

I'm a little unsure of the best way to resolve. Given that users shouldn't be extending the __constructor() method should we just make those final @acobster @gchtr?

@acobster
Copy link
Copy Markdown
Collaborator Author

Not sure I necessarily agree that users shouldn't be extending the constructor. We just don't want constructors being called directly, so that all instantiation happens via Class Maps and funneled through ::build(). It's probably a rare case that you actually need a custom constructor, but I could see some default object state being useful, so if we're going to cut off that possibility completely I'd at least want to know why.

Not really sure what PHPStan's issue with this actually is. I found the commit where they introduced this check, but no justification for it.

cc @szepeviktor

@szepeviktor
Copy link
Copy Markdown
Contributor

Please see https://3v4l.org/On7K3

@acobster
Copy link
Copy Markdown
Collaborator Author

acobster commented Feb 25, 2020

So the issue is that type declarations on a subclass's constructor might conflict with the declarations on the parent? I don't think that's an issue here, since our constructor takes no arguments. If people want to override __construct(), they can do so with zero arguments. Otherwise they will find out right away that required constructor args on CPT classes will cause a Fatal Error, and we can document this (rather advanced) use-case somewhere if we deem it necessary. So, to me this seems like a docs problem and not a safety issue. My proposal is to ignore the new static() rule in this case.

Thoughts?

@szepeviktor
Copy link
Copy Markdown
Contributor

will cause a Fatal Error

PHPStan is here with us to help prevent just that :)

@acobster
Copy link
Copy Markdown
Collaborator Author

Understood, and it's a great tool for that. :) All I'm saying is that at some point we need to look at the costs and benefits of following its rules to the letter. And in this specific instance - which I argue is outside the scope of the problem that example lays out - I believe the costs outweigh the benefits. We'll never be able to standardize our way out of every fatal error, and even if we prevent this one from happening, without a guide for how to do it the right way, our users will just stumble onto PHP Fatal error: Cannot override final method User::__construct(). Not a lot of benefit there from my perspective, so we might as well just provide a good advanced guide and keep each constructor as-is for flexilibity. So for now I'm standing by my recommendation of annotating these instances to be ignored and adding docs for when and how to override core constructors.

Would love to hear from @jarednova or @gchtr on this too!

@szepeviktor
Copy link
Copy Markdown
Contributor

szepeviktor commented Feb 26, 2020

PHPStan has a cost that may seem very high.
Although in reality PHPStan's (long-term) benefit greatly exceeds its cost.

@acobster
Copy link
Copy Markdown
Collaborator Author

I agree in principle, but I'm wondering what you see as being the long-term benefit in this particular case? Are you saying you disagree with my proposal? If so, why?

@szepeviktor
Copy link
Copy Markdown
Contributor

szepeviktor commented Feb 26, 2020

I run web applications for SaaS teams and daily see that they cannot make a sustainable product without these "basic" tests:

  1. syntax check
  2. coding standard
  3. static analysis (PHPStan Level 4+)

That is my daily observation.

@jarednova jarednova merged commit fa28781 into 2.x-factories Apr 14, 2020
@jarednova jarednova deleted the 2.x-coauthors-fixes branch April 14, 2020 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants