-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Deprecate Zend\Dom\Query in favor of more logical OO approach #5356
Conversation
return $this; | ||
} | ||
|
||
public function getType() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docblock missing
👍 nice refactoring. Maybe we have to keep this work for ZF3 ? cc @Maks3w |
@blanchonvincent It does qualify for a refactoring, I agree, but if we want to fully do this, we would need to change NodeList as well because I can't for the life of me figure out why it needs a cssQuery and an xPathQuery in the constructor. I would need to adress that as well as the CSS2Xpath class that should be a simple static method... But I'd really like to see this in the next ZF2 release though :( |
@ThomasCantonnet I think it's too important to have this in the next release. And, yes, I have the same opinion, you need to change NodeList (you can have cssQuery && xpathQuery when you use the queryCss, because the cssQuery is transformed to a xpathQuery :) ). |
* @param string|null $document | ||
* @param string|null $forcedType Type for the provided document (see constants) | ||
* @param string|null $forcedEncoding Encoding for the provided document | ||
* @return Document |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@return self
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup ok
@ThomasCantonnet Maybe you can have a Zend\Dom\Document (with encoding, errors, document transformation to domdocument) and a Zend\Dom\Query (cssQuery & xpathQuery) which will use a Zend\Dom\Document. Your code will be more decoupled and easier to test & maintain. |
@blanchonvincent Having an object just to store 2 properties and a NodeList seems like a lot of unncessary overhead to me, don't you think ? In this case all we need to do is rename NodeList to Query |
@ThomasCantonnet no because the "setStringDocument" and the "getDomDocument" logic will be moved into a same class, and the query logic will be on an other class. |
@ThomasCantonnet SRP = Single Responsibility Principle :) |
Can you give me more details on how you view this new architecture? I'm not sure I'm following. |
} | ||
|
||
return $this->domDocument; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When getDomDoument() called twice, getStringDocument() called twice ?
can be ?
if (null === $this->domDocument) {
if (null === ($stringDocument = $this->getStringDocument())) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getStringDocument() is a simple pointer to the property $stringDocument.
A few things to note:
|
@weierophinney Thanks for your input, I'll look into it during the week if I have time. On Dom\Query though, should I change all the functions while keeping BC or create a different class ? I'm not sure I can keep BC with the state of Dom\Query. |
@weierophinney @blanchonvincent Alright guys, deprecated a lot of classes but it looks much cleaner now :) I added a Query object and refactored the NodeList object, what do you guys think about it now? $document = new Document($html, Document::DOC_XHTML, 'UTF-8');
$query = new Document\Query('.foo', Document\Query::TYPE_CSS);
$result = $document->execute($query);
echo $result->count(); |
After reviewing, I agree with this. You can remove the factory method from NodeList, and then have the execute method of Query prepare the data to inject into the NodeList. |
I disagree, @blanchonvincent. Look at how PHP's DOM library works, particularly With that said, @ThomasCantonnet - it may make sense to make that mirroring even more clear, and maybe do the following:
Usage would then become: $document = new Document($contents);
$nodelist = Query($someExpression, $document, Query::TYPE_CSS);
foreach ($nodelist as $node) {
// ...
} Or, more succinctly, for those who like Perl golf: foreach (Query($someExpression, new Document($contents), Query::TYPE_CSS) as $node) {
// ...
} Both of which look quite clean to me. |
OK guys, I removed the NodeList factory method and changed Query::execute to be a static method with Matthew's proposition for a signature. Do we agree now ? :) |
Deprecate Zend\Dom\Query in favor of more logical OO approach Conflicts: tests/ZendTest/Dom/QueryTest.php
- Marked Css2Xpath::transform as deprecated. It now triggers an E_USER_DEPRECATED error and proxies to Zend\Dom\Document\Query::cssToXpath().
- Detailed the deprecation of `Zend\Dom\Css2Xpath::transform`
Merged to develop for release with 2.3.0. @ThomasCantonnet -- can you do a new PR that will update |
Sure, I'll see what I can do!
|
@ThomasCantonnet @weierophinney I already made the PR for fix Zend\Test. See #5356 |
Well in true I fixed the deprecated call which made ZendTest\Test tests fail |
README fix of Zend\Dom\Query (#5356)
The documentation needs an update. |
…net/query-deprecated Deprecate Zend\Dom\Query in favor of more logical OO approach Conflicts: tests/ZendTest/Dom/QueryTest.php
- Marked Css2Xpath::transform as deprecated. It now triggers an E_USER_DEPRECATED error and proxies to Zend\Dom\Document\Query::cssToXpath().
Fix typo added in 064fb125603dede6a85a2a0c5409a078c69a12b5
Issued introduced by 064fb125603dede6a85a2a0c5409a078c69a12b5
Css2Xpath is not longer tested by the file.
Hello everyone,
I am quite a heavy user of DOMDocument XPath queries and I have always been bothered by the Dom implementation in ZF2. The Zend\Dom\Query class seems very illogical as an object and contains quite a lot of inefficiencies and lacks access to the used DOMDocument generated from a string.
I created Zend\Dom\Document which makes more sense, because we are querying this object and not the other way around (creating a Query object that generates a DOMDocument which we then query and that we can't even access).
Zend\Dom\Query
class:Zend\Dom\Document
class:Problems solved:
The methods more or less stay the same, I reused the tests already available for Query and added some for the new behavior. I deprecated Zend\Dom\Query in favor of this new class.
I'm in favor of adding this to a ZF2.* future release because I consider this more like an a new feature (new logic & different object) than a component refactor. ZF3 is very far away and Zend\Dom is currently lacking a proper DOMDocument wrapper.