Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fixed issue #1898: API for querying the active mode(s) #733

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

johnstevenson
Copy link

Although it is possible to obtain the mode value in userland, it is
quite cumbersome and possibly brittle to do so. This is fixed with the
addition of an xdebug_mode() function:

xdebug_mode(?string $mode = null) : mixed

Returns the enabled xdebug modes as a comma-separated string, or if the
$mode parameter is supplied, whether the named mode(s) are set.

For example, using xdebug.mode=debug,develop,coverage.

var_dump(xdebug_mode());                  // string(22) "develop,coverage,debug"
var_dump(xdebug_mode('off'));             // bool(false)
var_dump(xdebug_mode('debug,coverage'));  // bool(true)
var_dump(xdebug_mode('trace,debug'));     // bool(false)

@johnstevenson
Copy link
Author

johnstevenson commented Mar 21, 2021

The failing test on PHP-8.1 (nightly) on Ubuntu is not related to this PR and occurs on xdebug:master as well:

TEST 7/738 [tests/base/bug00665.phpt]
========DIFF========
004- PASS4
006- PASS6
010- PASS10
012- PASS12
========DONE========
FAIL Test for bug #665: xdebug does not respect display_errors=stderr [tests/base/bug00665.phpt] 

This test passes on Windows nightly builds.

EDIT: This failed on PHP php/php-src@6690547 but now seems to be okay.

Although it is possible to obtain the mode value in userland, it is
quite cumbersome and possibly brittle to do so. This is fixed with the
addition of an `xdebug_mode()` function:

xdebug_mode(?string $mode = null) : mixed

Returns the enabled xdebug modes as a comma-separated string, or if the
`$mode` parameter is supplied, whether the named mode(s) are set.

For example, using `xdebug.mode=debug,develop,coverage`.

```
var_dump(xdebug_mode());                  // string(22) "develop,coverage,debug"
var_dump(xdebug_mode('off'));             // bool(false)
var_dump(xdebug_mode('debug,coverage'));  // bool(true)
var_dump(xdebug_mode('trace,debug'));     // bool(false)
```
@johnstevenson
Copy link
Author

Derek wrote:

This is a duplicate of 0001898, which I'll implement for 3.1. I'm still pondering how to make this more useful than just returning a list of enabled modes though.

I've updated the commit message to match the already-reported issue. Please consider this PR as an example of such a function and do with it as you wish.

@johnstevenson johnstevenson changed the title Fixed issue #1959: Add a function to obtain xdebug mode Fixed issue #1898: API for querying the active mode(s) Mar 22, 2021
xdebug_str_add_literal(&mode_out, "off");
} else {
if (XDEBUG_MODE_IS(XDEBUG_MODE_DEVELOP)) {
xdebug_str_add_literal(&mode_out, "develop,");
Copy link
Contributor

Choose a reason for hiding this comment

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

an array would be better

Copy link
Author

Choose a reason for hiding this comment

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

I'm sure Derek is going to do his own thing here, but why do you think that an array would be better?

Copy link
Contributor

Choose a reason for hiding this comment

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

well, to me, an array of strings seems easier to understand than a single string, where we need to remember that the string actually contains multiple modes in a comma-separated way (for which a check would probably be done by exploding the string anyway)

Copy link
Author

Choose a reason for hiding this comment

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

@stof Yeah, I get that. But you don't need to explode the string to do an in_array() check, because you already have xdebug_mode('modes,you,want,to,check') functionality which returns true/false.

I have assumed that usage would be either to check a value (as above), or to set a value(s) in a new process - which you can do simply with $mode = xdebug_mode().',modes,you,need'; etc, rather than having to implode an array.

I also thought that if you returned an array of values, you should really only accept an arrary of values, for consistency, which seemed cumbersome if you only wanted to check a single value.

Copy link
Contributor

@mvorisek mvorisek Apr 13, 2021

Choose a reason for hiding this comment

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

xdebug_mode().',modes,you,need' is one example why you want array ;-)

Clean code is array_unique([...xdebug_mode(), 'modes', 'you', 'need']) :)

Copy link
Author

Choose a reason for hiding this comment

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

@mvorisek Why? It doesn't matter (each value is just OR-ed to the current value) and is consistent with how the ini and env values are parsed/used.

@stof
Copy link
Contributor

stof commented Apr 13, 2021

should this be a single function having 2 totally different behavior depending on whether it is called with 1 or 2 parameters, or 2 separate functions ?

@stof
Copy link
Contributor

stof commented Apr 13, 2021

we could have xdebug_mode_is_active(string $mode): bool for the case of checking whether one mode is active.

@derickr
Copy link
Contributor

derickr commented Apr 13, 2021

What I likely want to do is to extend xdebug_info() with an (optional) argument that returns information based on that argument.

Right now, xdebug_info() returns nothing, and instead displays a whole lot of information. Much of that information could be useful for applications that need to do something with it, such as checking modes. I don't really want to add a separate function for each of these "feature" requests, and hence likely want to go with:

xdebug_info('modes');, which will then return an array of which modes are enabled, and whether they have been activated. Likely returning an array as:

[
    'develop' => 'active',   // develop is always either 'active' or 'disabled', as there is no session associated with it
    'debug' => 'enabled',   // debug was turned on, but no debugging session active
    'coverage' => 'disabled', // coverage wasn't enabled at all
]

Alternatively I could make multiple arguments to xdebug_info() to make this not a tri-state, such as allowing for:
xdebug_info('enabled_modes') and xdebug_info('active_modes');.

And then perhaps later I can extend this with xdebug_info('profile') to give information about the current profiling session, perhaps to include the file name that the profile is written too (now you can use xdebug_get_profile_name() for that. etc etc.

@derickr
Copy link
Contributor

derickr commented Apr 21, 2021

I have for now settled on:

$ XDEBUG_MODE=debug,coverage php -dxdebug.mode=trace -r 'var_dump(xdebug_info("mode"));'
array(2) {
  [0]=>
  string(8) "coverage"
  [1]=>
  string(5) "debug"
}

Let me know if that works. With that, I'll close this PR and y'all can follow #737

@derickr derickr closed this Apr 21, 2021
@johnstevenson
Copy link
Author

Thanks @derickr Seems like a nice uncomplicated solution.

@johnstevenson johnstevenson deleted the mode branch April 21, 2021 17:26
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.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.