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

if a tag does not exist, we dont want the query to crash #61

Merged
merged 4 commits into from
Jun 3, 2019

Conversation

tom-it
Copy link
Contributor

@tom-it tom-it commented May 25, 2019

No description provided.

@tom-it
Copy link
Contributor Author

tom-it commented May 25, 2019

I'm currently working on a flarum implementation, and noticed the tags search would fail when a tag was entered that did not exist.

this works:
https://discuss.flarum.org/?q=tag%3Asupport%20tag%3Ainstallation

this does not work:
https://discuss.flarum.org/?q=tag%3Asupport%20tag%3Ainstallationabc

this pull request will fix the query to return an empty result

@tom-it
Copy link
Contributor Author

tom-it commented May 25, 2019

not sure why one test failed..

@luceos
Copy link
Member

luceos commented May 26, 2019

Because your conditional markup is incorrect, see: https://github.styleci.io/analyses/zdBJZv

@tom-it
Copy link
Contributor Author

tom-it commented May 26, 2019

Strange, looks fine to me in the commit 6680e5a

@clarkwinkelmann
Copy link
Member

clarkwinkelmann commented May 28, 2019

@tom-it it's missing the space between if (. Also == true is unnecessary as isEmpty() returns a boolean already 😉

Also, indentation is wrong. I think StyleCI gives you the patch to make the necessary changes.

@tom-it
Copy link
Contributor Author

tom-it commented May 28, 2019

@tom-it it's missing the space between if (. Also == true is unnecessary as isEmpty() returns a boolean already 😉

Also, indentation is wrong. I think StyleCI gives you the patch to make the necessary changes.

Ah Thanks, will check it out!

adding the == true is a personal preference, but I understand that the code needs to be uniform to prevent different styles from cluttering up the codebase.

@luceos
Copy link
Member

luceos commented May 28, 2019

I was looking at this PR and - although solving it is absolutely a requirement - the approach is off.

The problem here is that the repository method getIdForSlug is returning a Collection instance, instead it should return an int as also shown in the phpdoc of that method. This is a result of an upgrade to Laravel 5.7 components a while ago and possibly a bug from before that.

I'd like to propose the following solution and hoping you, @tom-it, would like to give this a shot:

  • Update the getIdForSlug with return type hinting : ?int so that the method will always error when something different than an integer or null is returned.
  • Update the getIdForSlug to do the isEmpty check, return null if so, otherwise return the first result and its id property. You could potentially use firstOrNew so that you get an empty object if no entry exists, that would make id null or int by default (I think).

Thanks for your first ever PR to this project!

@tom-it
Copy link
Contributor Author

tom-it commented May 28, 2019

I was looking at this PR and - although solving it is absolutely a requirement - the approach is off.

The problem here is that the repository method getIdForSlug is returning a Collection instance, instead it should return an int as also shown in the phpdoc of that method. This is a result of an upgrade to Laravel 5.7 components a while ago and possibly a bug from before that.

I'd like to propose the following solution and hoping you, @tom-it, would like to give this a shot:

  • Update the getIdForSlug with return type hinting : ?int so that the method will always error when something different than an integer or null is returned.
  • Update the getIdForSlug to do the isEmpty check, return null if so, otherwise return the first result and its id property. You could potentially use firstOrNew so that you get an empty object if no entry exists, that would make id null or int by default (I think).

Thanks for your first ever PR to this project!

Sure thing, I will check it out tonight.

I'm currently working on a flarum project so I will be doing some more PR's on the main flarum project as well, and see where I can help out on any open issues.

@luceos
Copy link
Member

luceos commented May 28, 2019

That would be great Thomas and I love seeing another 🇳🇱around here ;)

If you have any questions, we're on discord most of the time http://flarum.org/discord/

Revert "if a tag does not exist, we dont want the query to crash"

This reverts commit 6680e5a.
@tom-it
Copy link
Contributor Author

tom-it commented May 28, 2019

That would be great Thomas and I love seeing another 🇳🇱around here ;)

If you have any questions, we're on discord most of the time http://flarum.org/discord/

I have added the functionality as described, still styleci doesn't like my commit, do I need to remove the new lines?

src/TagRepository.php Outdated Show resolved Hide resolved
@tom-it
Copy link
Contributor Author

tom-it commented May 29, 2019

Let me know if you need anything else for this.

@tobyzerner
Copy link
Contributor

Looks good, thanks @tom-it. And the original issue is still fixed?

@tom-it
Copy link
Contributor Author

tom-it commented May 29, 2019

@tobyzerner correct tested and working

@luceos
Copy link
Member

luceos commented May 29, 2019

The original issue is also guaranteed to be fixed because of the return type hinting. A Collection instance would trigger an error.

Great job Thomas!

@franzliedke franzliedke merged commit 3d6921b into flarum:master Jun 3, 2019
askvortsov1 pushed a commit that referenced this pull request Mar 11, 2022
* if a tag does not exist, we dont want the query to crash

* incorrect solution
Revert "if a tag does not exist, we dont want the query to crash"

This reverts commit 6680e5a.

* repaired getIdForSlug function to return int or null

* changed where, removed isempty not needed
askvortsov1 pushed a commit that referenced this pull request May 10, 2022
* if a tag does not exist, we dont want the query to crash

* incorrect solution
Revert "if a tag does not exist, we dont want the query to crash"

This reverts commit 6680e5a.

* repaired getIdForSlug function to return int or null

* changed where, removed isempty not needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants