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

Fix root option on TaxonChoiceType #7254

Merged

Conversation

Pitoune
Copy link

@Pitoune Pitoune commented Jan 13, 2017

Q A
Bug fix? yes
New feature? no
BC breaks? yes
Related tickets
License MIT

@@ -81,7 +80,14 @@ public function configureOptions(OptionsResolver $resolver)
$resolver
->setDefaults([
'choices' => function (Options $options) {
$taxons = $this->taxonRepository->findNodesTreeSorted();
if (null !== $options['root']) {
if ($options['root'] instanceof TaxonInterface) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it too much magic? Why don't we stay with only taxon code in root?

@@ -81,7 +80,14 @@ public function configureOptions(OptionsResolver $resolver)
$resolver
->setDefaults([
'choices' => function (Options $options) {
$taxons = $this->taxonRepository->findNodesTreeSorted();
if (null !== $options['root']) {
Copy link
Member

Choose a reason for hiding this comment

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

Could be extracted to private method

@@ -43,6 +42,24 @@ public function findChildren($parentCode)
/**
* {@inheritdoc}
*/
public function findChildrenByRootCode($rootCode)
{
$queryBuilder = $this->createQueryBuilder('o');
Copy link
Member

Choose a reason for hiding this comment

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

Redundant

@Pitoune Pitoune force-pushed the feat/taxon-choice-type-root-option branch from 783f707 to 910bb12 Compare January 16, 2017 09:59
->leftJoin('o.root', 'root')
->andWhere('root.code = :code')
;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid this nasty else statement. Would be a good to extract this to private method. But also there is a lot of logic in this if... So maybe two different methods, findChildren and findChildrenForRootParent?

Copy link
Author

Choose a reason for hiding this comment

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

That's what I did in the first place but @lchrusciel said the method findChildrenForRootParent was redundant with the findChildren one.

Copy link
Member

@lchrusciel lchrusciel Jan 17, 2017

Choose a reason for hiding this comment

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

Sorry, I wasn't clear enough... I meant that the variable assignment is redundant... Sorry about that :)

return $this->createQueryBuilder('o') 
    ->addSelect('child')
    ->leftJoin('o.children', 'child')
    ->leftJoin('o.parent', 'parent')
    ->andWhere('parent.code = :code')
;


/**
* @param null|string $root
* @param null|callable $filter
Copy link
Member

Choose a reason for hiding this comment

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

string|null and callable|null. Also we should not align parameters vertically.

* @param null|string $root
* @param null|callable $filter
*
* @return array|TaxonInterface[]
Copy link
Member

Choose a reason for hiding this comment

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

TaxonInterface[] would be sufficient, array| indicates as there could be something else ;)

{
if (null !== $root) {
$taxons = $this->taxonRepository->findChildren($root, true);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

I know that another extraction would not be a good option (two methods for a simple case), but else does not look good too :/ How could we make it shiny and perfect?

Copy link
Author

Choose a reason for hiding this comment

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

What about adding a nullable argument $rootCode to method findNodesTreeSorted ? It means putting some logic in the repository but we only need an if without else.

@@ -25,10 +25,11 @@
{
/**
* @param string $parentCode
* @param bool $isParentRoot
Copy link
Member

Choose a reason for hiding this comment

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

Again, do not indent parameters vertically.

@@ -23,20 +22,30 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa
/**
* {@inheritdoc}
*/
public function findChildren($parentCode)
public function findChildren($parentCode, $isParentRoot = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this parameter?

Copy link
Contributor

Choose a reason for hiding this comment

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

findChildren('CODE', true) shouldn't return grandchildren etc.
It should be rather findChildren($parentCode) and findDescendants($rootCode).

@Pitoune Pitoune force-pushed the feat/taxon-choice-type-root-option branch 4 times, most recently from 5308eda to 5f907b2 Compare January 17, 2017 11:21
}

return $taxons;
return $this->getTaxons($options['root'], $options['filter']);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rename this option to root_code to avoid confusion.

@Pitoune Pitoune force-pushed the feat/taxon-choice-type-root-option branch from 5f907b2 to d17ad26 Compare January 18, 2017 12:09
@Pitoune Pitoune force-pushed the feat/taxon-choice-type-root-option branch from d17ad26 to 8e9a2bb Compare January 30, 2017 08:50
@Pitoune
Copy link
Author

Pitoune commented Jan 30, 2017

@michalmarcinkowski did you have time to have a look on my last commit?

@pjedrzejewski pjedrzejewski added the BC Break PRs introducing BC breaks (do not even try to merge). label Jan 30, 2017
@michalmarcinkowski michalmarcinkowski merged commit 367bad5 into Sylius:master Jan 30, 2017
@michalmarcinkowski
Copy link
Contributor

I'm not a fan of optional arguments, but creating a separate method for that doesn't seem a better choice. Thanks Pierre! 👍

@Pitoune Pitoune deleted the feat/taxon-choice-type-root-option branch January 31, 2017 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BC Break PRs introducing BC breaks (do not even try to merge).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants