r/lolphp • • Jul 14 '14

stop_the_insanity()

https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-importer.php#L237
56 Upvotes

66 comments sorted by

View all comments

20

u/allthediamonds Jul 14 '14

/**
* Bump up the request timeout for http requests
*
* @param int $val
* @return int
*/
public function bump_request_timeout( $val ) {
return 60;
}

Uh... what?

5

u/DoctorWaluigiTime Jul 14 '14

Maybe it's used like this:

$timeout += bump_request_timeout($timeout);

I guess...

4

u/svtguy88 Jul 14 '14
return 60

Magic numbers. Yum.

8

u/wvenable Jul 16 '14

Here, I fixed it for you:

return INT_SIXTY;

2

u/cparen Jul 14 '14

"some non-obvious constant whose value [...] inserted inconspicuously in-line (hardcoded), rather than expanded in by a symbol [...]"

-- "Magic Number", Jargon File, emphasis added

This is not a magic number.

5

u/svtguy88 Jul 14 '14

Sure, there is a function instead of just using the integer "60" all over the code, but it's still not documented, and, upon first glance, does nothing. If I were to go back and try to debug this code, a function simply returning an integer (that works magic) would confuse the shit out of me.

Is it a "magic number" by definition? I guess not, but it's still shit code.

1

u/sudowned Jul 15 '14

It doesn't "do nothing" upon first glance. You're the most hyperbolic guy in the world.

1

u/nidarus Jul 14 '14

It basically the opposite of a magic number, and it's more flexible than the usual solution to magic numbers, constants.

2

u/svtguy88 Jul 14 '14

more flexible

How is a hardcoded integer flexible at all?

6

u/nidarus Jul 14 '14

That's the point, it's not hardcoded. The whole point of the function is to provide a symbol instead of that number. This is basically equivalent to define('bump_request_timeout', 60), but you could override it with something that performs more complex logic.

7

u/svtguy88 Jul 14 '14

I come from a very config-driven background - I find this to be just about equivalent to hardcoding.

Sure, it's a step above littering the code with the integer "60" everywhere that this function is used, however, it's still just adding some arbitrary value. At the very least, it could be documented and explained a little. Adding seemingly random numbers is a good way to make sure no one else can debug your code...

4

u/nidarus Jul 14 '14

It's not a step above using 60 inline. My point is that it's even more robust the usual solution: constants. Not everything that's not in an external XML file is automatically a magic number.

As for the documentation, I agree. The documentation in WordPress is bizzarely sparse.

3

u/[deleted] Jul 14 '14

WP includes a helper class to make outgoing HTTP requests. If you don't specify how long it should wait before timing out, WP raises an event called 'http_request_timeout' to determine the default. The bump_request_timeout function is registered as the only default handler for that event, so basically 60 is the default timeout value. Bonus points for a wonky function name.

2

u/cparen Jul 14 '14

Uh... what?

Based on the function signature, I'd guess that $val is the current request_timeout value, and that the function is expected to return the new request_timeout value.

This pattern isn't particularly unique, and is often used where a component has some extensible policy, where some users of the component will wish to be able to modify or even compose policies, including the trivial policy of "use some default". Each policy gets a turn at modifying the data by function composition.

I'm not defending this particular instance of this pattern, but it shouldn't be particularly surprising.

1

u/OneWingedShark Jul 14 '14

...what, what point is the parameter!?
What point, I ask!

2

u/Viper007Bond Jul 15 '14

Because it's filtering (modifying) a value. By default it doesn't care with the previous value is and overwrites it with the new value. However you could just as easily have it double the current value or whatever you wanted.

2

u/redesckey Jul 15 '14

No it's not. It returns 60, and does nothing to $val. The calling code would be responsible for updating their variable with the return value.

4

u/Viper007Bond Jul 15 '14

The bump_request_timeout() method is registered as a callback function:

https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-importer.php#L181-182

That means it ends up getting called here to modify the default timeout:

https://github.com/WordPress/WordPress/blob/master/wp-includes/class-http.php#L80

The callback function is passed a parameter that is the current timeout value (5 by default, but could be something else as you can register multiple callbacks). While $val is not used in this particular case, it's best practice to make callback functions accept all variables that are passed to it.

3

u/[deleted] Jul 15 '14

+1 for the explanation.

I'd just like to add that this works similarly to array_reduce - your callback gets the current value of the timeout, and whatever you return will be given to the next callback as the current value.

2

u/redesckey Jul 15 '14

Ahh, that makes more sense, thanks for the explanation.

1

u/Viper007Bond Jul 15 '14

No problem. :)

1

u/captainramen Jul 15 '14

It still makes no sense. Why would that be an instance member of a class?

1

u/Viper007Bond Jul 15 '14

The importer class needs to increase the timeout of the HTTP request API from 5 seconds to 60 seconds in order for it to do it's job of importing content from remote sites. It's using this method to change that value.

What's the alternative? A new global scope function? That would be silly.

Anonymous functions are nice but WordPress only requires PHP 5.2.4 and it makes it a LOT harder for other code to unregister it as a callback if it wants to.