GithubHelp home page GithubHelp logo

Comments (8)

gmazzap avatar gmazzap commented on September 6, 2024 1

At the moment it breaks, yes.

I implemented this because it was discussed in last call, at seemed there was consensus on this.

I can tell that being a constant you can leverage "find usages" which -at least- prevents you to search manually for where the hook is fired.

But I guess we can open an issue on phpStorm to ask them to implement search hook by constant, that should not be hard for them.

from wonolog.

gmazzap avatar gmazzap commented on September 6, 2024

Pretty much all hoks names are no in class constants. The "main" hook wonolog.log is in the constant Inpsyde\Wonolog\LOG so it does no belog to any class. In PHP 5.6+ with use constant this hook can be fired like do_action(LOG, ... ) which is quite fine, I think.

Existing hooks won't break because the constants use same string, but is suggested to replace theme.

from wonolog.

dnaber-de avatar dnaber-de commented on September 6, 2024

How does PHPStorm behave on that? Doesn't that break the integration of the WP Hook system in a case that I can't easily ยซjumpยป to an origin of a hook form the place it is assigned?

from wonolog.

dnaber-de avatar dnaber-de commented on September 6, 2024

I implemented this because it was discussed in last call, at seemed there was consensus on this.

This seems to passed me completely ๐Ÿ˜•
What was the benefit again?

I can tell that being a constant you can leverage "find usages" which -at least- prevents you to search manually for where the hook is fired.

At least I cannot use the constant directly in an independent plugin, as this would completely negate the flexibility of hooks that are applied/fired if available but nothing happens if for example Wonolog isn't available. Maybe I get something wrong here?

from wonolog.

gmazzap avatar gmazzap commented on September 6, 2024

I get something wrong here?

No, you don't.

When we talked, @tfrommen pointed out that hooks that are used in many places in the plugin should be wrote in a constant, and I agreed.

Reason why I agreed is that it is hard to remeber hooks (expecially long named hooks) and it's easy to fat-finger an hook name. Moreover, more generally speaking, to have core business rules that rely on a weak type like string is wrong IMO, more so on a package that supports PHP 5 where string can't event be enforced as type, so I always prefer to have a constant or even a value object instead of strings when they affect or dictate business logic.

Is it also true what you say: phpStorm WordPress integration is better (at least, for now) when you use hardcoded strings for hooks.

Now, I like phpStorm and its WP integration, but I value more compliance with better code (or my idea of better code) than a specific IDE helpers. Even because if there's an issue with IDE it should be fixed in the IDE and should not affect the code, IMO.

This is my opinion, if there's a consensus against it I am ok to revert this change.

from wonolog.

dnaber-de avatar dnaber-de commented on September 6, 2024

This is my opinion, if there's a consensus against it I am ok to revert this change.

Please don't. But before we deploy it to other projects, we should talk about it with more people, I think. I'm still confused that I missed that part of the meeting out. :/

When we talked, @tfrommen pointed out that hooks that are used in many places in the plugin should be wrote in a constant, and I agreed.

If you use a hook name more than once it might be a good idea to capsule it in a function or better in an API method. That also solves all the other problems like weak types or fat-finger hook names.

from wonolog.

gmazzap avatar gmazzap commented on September 6, 2024

If you use a hook name more than once it might be a good idea to capsule it in a function or better in an API method. That also solves all the other problems like weak types or fat-finger hook names.

We get back to initial issue: API forces dependency to Wonolog. Directly triggering the action, doesn't.

Is it true that in other places you can't use the constant, but in other plugins you probably don't get the IDE helper either, so you might need write it manually (or copy and paste from documentantation) either way.

If you have an API you could do

function_exists( 'Inpsyde\wonolog_log' ) and Inpsyde\wonolog_log( ... )

but at this point why not

defined( 'Inspyde\Wonolog\LOG' ) and do_action( Inspyde\Wonolog\LOG ... )

(the latter is also faster)

But being an external plugin, I see no problems in just do do_action( 'wonolog.log' ... ) (this is what would be documented).

Using a constant is something that makes package (Wonolog) code better... how consuming code make uses of the package is not up to the package.

For example, a package could define own function to trigger wonolog log actions, and that that would be totally fine. For example:

namespace My\Package;

function log( $log_data ) {

  static $wonolog_hook;
  if ( ! isset($wonolog_hook) ) {
    $wonolog_hook = defined('Inspyde\Wonolog\LOG') ? Inspyde\Wonolog\LOG : '';
  } 

  if ($wonolog_hook) {
    do_action( $wonolog_hook, $log_data );
    return;
  }

  error_log( json_encode( $log_data ) );
}

But this is something that is up to consumers, and not to wonolog, IMO.

from wonolog.

tfrommen avatar tfrommen commented on September 6, 2024

Basically, everything is said already. :)

That some string is available through a class constant that you can reference, does not mean at all that you only can reference the value via the constant. You can (and are good to) still use the string directly.

If you want to reference the class constant (and you didn't check for Wonolog being active before), then you have to make sure you can do this. For example, just like @gmazzap illustrated, by using defined() and do_action().

The benefit is that you have the value in a single place. If it ever were to change, you only have to update the constant - and all code that already references the constant will work just fine.
I like to do this thing on interfaces. They provide action and/or filter hook names that implementors can (and should) use. So, it's not always that you have multiple parties use this in parallel - but maybe one out of several possible versions. Having some wrapper function in this case would just be an unnecessary overhead (although negligible, of course).
You also get auto-complete in your IDE.
And lastly, your test can directly use the class constants - and doesn't have to be adapted anytime.

But: yes, it would also be cool to teach PhpStorm constant action/filter look-up. I will have a look at how best to do this - maybe only open a youTrack issue.

from wonolog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.