[4.0] Document: deprecate direct use of $_scripts and $_styleSheets#25301
[4.0] Document: deprecate direct use of $_scripts and $_styleSheets#25301Fedik wants to merge 7 commits intojoomla:4.0-devfrom
Conversation
| } | ||
|
|
||
| // Merge with previously added scripts | ||
| $doc->_scripts = array_replace($doc->_scripts, $jsBackup); |
There was a problem hiding this comment.
This isn't right, you're changing a replace operation to an append operation.
There was a problem hiding this comment.
not really, I just changed how it looks like 😉
But result are the same: addScript() will do the same as array_replace(), if the $url exists, then it "replace", otherwise will "append"
There was a problem hiding this comment.
You're relying on a non-documented implementation detail to make this claim. The interface does not document whether the expected behavior is to overwrite an existing entry or raise an error, therefore if someone opted to subclass the Document class and throw an Exception if someone tries to register a URL twice, this assumption is broken.
At the end of the day, you're still changing the logic behind all of this. Right now it is crystal clear you are doing a replace operation. This refactoring looses clarity and proves some of the issues others have highlighted with this PR making an arbitrary assumption that the properties should only have getters.
There was a problem hiding this comment.
Indeed if the direct use of public properties $_scripts and $_styleSheets is removed, at least there must be both getters and setters for those properties. Otherwise it won't be no longer possible to manipulate loaded scripts and stylesheets.
There was a problem hiding this comment.
The interface does not document whether the expected behavior is to overwrite an existing entry or raise an error
I think, if there no throws tag in a "method description" then there should not be Exception.
if someone throws an Exception then his implementation of the method are incorrect, no?
And duplication should be avoided by Document class, in one or another way, anyway.
There was a problem hiding this comment.
Not every throwing method has a documented @throws annotation, and actually a lot of interfaces in general fail to document "allowed" exception types (most of this isn't only related to Joomla, but since most people here don't touch PHP outside of it...). Likewise, not every method has @throws annotations if it doesn't catch exceptions from methods it calls (you then get into a debate whether the method should document only the exceptions it natively throws or whether it should document both those it throws and those that may be thrown by methods it calls, and at that point good luck keeping up with things). Therefore, relying on @throws annotations to indicate whether a method is allowed to throw an exception is not practical.
Either way, the point still stands there is no documented interface behavior, there is only the present de facto standard in the root class (which if this is what people want the method to do, that's fine, document it as the expected interface behavior (as that's exactly what an API specification is), but don't rely on "well this is what the root class does now so this is what will always happen"). Doc blocks are supposed to be more useful than "Adds a linked script to the page", but people are afraid to write useful documentation and instead view doc blocks as something that's just checking the block to pass PHPCS and write useless and doc blocks that are nothing more than a repetition of what the method signature says.
There was a problem hiding this comment.
@Fedik just document in the method description the behaviour. I think the chances of people overriding this is fairly low and I'm happy to just make that description explicit rather than being an implementation detail.
There was a problem hiding this comment.
@wilsonge I have already updated it in the last commit 😉
|
This PR is incomplete and not the equivalent of having access to $_scripts, $_script and $_styleSheets, $_style. 99% of cases we want to access $_scripts or $_styleSheets to manipulate or remove some file such as: So if you want to make these properties as 'protected' in a future release and have a getter method, you should at least have even a related setter method. As a developer i still want to continue to remove a loaded script or stylesheet from the document as it's possible now. |
|
If we start deprecating them, then please add the functions like getScript to the HTMLDocument class. We should not bind the generic Document class to HTML stuff. |
|
Scripts and stylesheets are not unique to HTML output only. You can use stylesheets within XML documents, as an example. |
|
But then it would be better to make a base class for script based documents. |
|
It'd be better to use the decorator pattern in all honesty because there are a number of properties and methods that do not apply to all output formats, but then you are forcing a lot of downstream code to introduce |
|
(Or, just drop everything but HTML from core like I've lightly suggested a number of times because nobody wants to make any non-HTML outputs proper first class citizens and everyone just assumes Joomla will always print HTML or bypasses core if they do non-HTML to the point where it doesn't matter how broken core is) |
|
|
||
| /** | ||
| * Adds a linked script to the page | ||
| * Adds a linked script to the page. If the script already exists then merges its options and attributes. |
I can add removeScript, removeStyleshet etc |
Yes that would be perfect. This is something that has always been missing in the Document API and in past years developers have been forced to use unset on the public properties. |
I will add in the weekend |
|
drone said
I not sure that it related to the PR |
|
hold on this, for now |
Summary of Changes
Deprecate direct use of
$_scripts,$_scriptand$_styleSheets,$_style.Introduce
Document::getScripts()Document::getStyleSheets()etc.Testing Instructions
Apply the patch and make sure everything still works as before the patch.
Documentation Changes Required
If
$_scripts,$_scriptand$_styleSheets,$_stylementioned somewhere, then it should be changed toDocument::getScripts(),Document::getScriptDeclarations(),Document::getStyleSheets(),Document::getStyleDeclarations().