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
Extracted the IP class into a standalone Network component #6517
Conversation
Nice to see this PR! 👍 Here is feedback: +1 for renaming P2N / N2P
maybe you have a suggestion where we those methods could be moved to, or do you think it is better to leave them in IP class for now?
Sounds good that you thought of that. When you searched for occurrence in the codebase, you can search in all repos not only piwik/piwik. you can use |
Good point, will do that! |
@mattab I think I'll leave the plugins call the deprecated code, we can change tehm later when we have solid continuous integration and tests passing (some don't have tests). We will anyway have a way to know when plugins are calling deprecated methods, so I think it's not worth delaying this PR. The sooner it's in the main branch, the more confident we'll be about it. |
So except that, this PR is good to merge to me now. |
Excellent Pull request :)
👍
+1 refs #6539 |
Extracted the IP class into a standalone Network component
OK following #6494 this is the PR for the extraction of the
Piwik\IP
class into the Network component. I have also included IP anonymization.Feedback is welcome!
Classes
I moved most methods into a new
IP
class that is not static anymore, it's a value object. Usage example:As you can see, there are factory methods in the base
IP
class. Then there are 2 implementations: IPv4 and IPv6. I know at first it sounds like unnecessary to split it in 3 classes like that, but it actually makes a lot of sense and the code is better.Take IP anonymization for example. With a single
IP
class:With
IP
+IPv4
andIPv6
classes:Polymorphism FTW!
In general, everywhere
isIPv4()
orisIPv6()
was used, it was to implement a custom behavior. That's what OOP is for. So that's (IMO) a good replacement. And there's no impact to the user, only better code and better tests.There were also some methods that didn't belong in an IP object, so I put them in a
IPUtils
class:Naming
As a matter of naming, I have replaced the
P2N()
andN2P()
notation with something more explicit: binary format and string format. After looking online, "presentation format" and "network address format" where not so common terms. Hope it's OK, let me know if you have better ideas. It can be changed.Namespace
In the new component we have now:
Piwik\Network\IP
Piwik\Network\IPv4
Piwik\Network\IPv6
Piwik\Network\IPUtils
Should it be moved to the subnamespace
Piwik\Network\IP\
?I'm wondering if it's necessary if someday we add other classes to that component.
Deprecated old
Piwik\IP
classI have deprecated all the methods of the old IP class that I have moved to the new component. I have also replaced almost all the usages of the old IP class by the new ones throughout the codebase. So
Piwik\IP
class is almost never used anymore.However some methods where not moved:
getIpFromHeader()
getNonProxyIpFromHeader()
getLastIpFromList($csv)
Those are either specific to the current request or just too specific at all.
TODO
composer.json
to use a tagged version and remove the URL to the git repositoryPiwik\IP
in public and pro plugins?