Skip to content

move the getEntityManager, only get it if needed #5828

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

Closed
wants to merge 1 commit into from

Conversation

OskarStark
Copy link
Contributor

if this will be merged, please be sure, that this is the correct branch!

Thank you

@@ -135,10 +135,10 @@ a ``postPersist`` method, which will be called when the event is dispatched::
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();

// perhaps you only want to act on some "Product" entity
if ($entity instanceof Product) {
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 prefer returning early.

if (!$entity instanceof Product) { return; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is not so clear IMO, maybe you want to check for another Type later?

Copy link
Contributor

Choose a reason for hiding this comment

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

returning early makes the code much more readable.
http://programmers.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement

it's not part of the symfony Styleguide but this PR is about readability.

if you want to check for multiple types you probably violate the SRP, so just don't to it :)

Copy link
Member

Choose a reason for hiding this comment

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

While the guard style is nice, I think the current syntax is more clear for people not familair with this style of guarding (and I assume lots of beginners are not familair with this syntax).

Copy link
Member

Choose a reason for hiding this comment

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

I generally would be pro "returning early" - i.e. reversing the logic here. I see too many nested if statements in people's code - teaching them to return is a tiny win.

Copy link
Member

Choose a reason for hiding this comment

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

I am in favour of return early here too.

Copy link
Member

Choose a reason for hiding this comment

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

See fab0985

@wouterj
Copy link
Member

wouterj commented Nov 8, 2015

👍

I think we should add a new line before the // ... comment, but that's such a minor thing that it can be done while merging.

@weaverryan
Copy link
Member

Looks fine to me 👍

@wouterj
Copy link
Member

wouterj commented Nov 25, 2015

Thanks Oskar.

wouterj added a commit that referenced this pull request Nov 25, 2015
…ark)

This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #5828).

Discussion
----------

move the getEntityManager, only get it if needed

if this will be merged, please be sure, that this is the correct branch!

Thank you

Commits
-------

1498140 move the getEntityManager, only get it if needed
wouterj added a commit that referenced this pull request Nov 25, 2015
@wouterj wouterj closed this Nov 25, 2015
weaverryan added a commit that referenced this pull request Nov 27, 2015
* origin/2.7:
  [#5828] Return early
  move the getEntityManager, only get it if needed
  Update front controller
  Tell about SYMFONY__TEMPLATING__HELPER__CODE__FILE_LINK_FORMAT
  Added new security advisories to the docs
  Fixed some wrong line numbers in doctrine.rst
  [Book][Templating] Update absolute URL asset to match 2.7
  Update debug_formatter.rst
  Removed the use of ContainerAware class
  Book: Update Service Container Documentation
  Removed an outdate paragraph
  minor lang tweak
  Fixes done automatically by the docbot
  updated sentence
  Removed "http_basic" config from the login form cookbook
@OskarStark OskarStark deleted the patch-5 branch June 17, 2019 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants