Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Debug] Developer friendly Class Not Found and Undefined Function errors #8553

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

Merged
merged 8 commits into from
Jul 29, 2013

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Jul 24, 2013

This is a followup of #8156

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #8156
License MIT
Doc PR n/a

Here is the original description from #8156:

Per a discussion with @weaverryan and others, I took a crack at enhancing the exception display for class not found errors and undefined function errors. It is not the cleanest solution but this is a work in progress to see whether or not following this path makes sense.

Class Names

Class Not Found: Unknown Class (Global Namespace)

<?php
new WhizBangFactory();

Attempted to load class 'WhizBangFactory' from the global namespace in foo.php line 12. Did you forget a use statement for this class?

Class Not Found: Unknown Class (Full Namespace)

<?php
namespace Foo\Bar;
new WhizBangFactory();

Attempted to load class 'WhizBangFactory' from namespace 'Foo\Bar' in foo.php line 12. Do you need to 'use' it from another namespace?

Class Not Found: Well Known Class (Global Namespace)

<?php
new Request();

Attempted to load class 'Request' from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add 'use Symfony\Component\HttpFoundation\Request' at the top of this file?

Class Not Found: Well Known Class (Full Namespace)

<?php
namespace Foo\Bar;
new Request();

Attempted to load class 'Request' from namespace 'Foo\Bar' in foo.php line 12. Do you need to 'use' it from another namespace? Perhaps you need to add 'use Symfony\Component\HttpFoundation\Request' at the top of this file?

Functions

Undefined Function (Global Namespace)

<?php
// example.php:
// namespace Acme\Example;
// function test_namespaced_function()
// {
// }
include "example.php";

test_namespaced_function()

Attempted to call function 'test_namespaced_function' from the global namespace in foo.php line 12. Did you mean to call: '\acme\example\test_namespaced_function'?

Undefined Function (Full Namespace)

<?php
namespace Foo\Bar\Baz;

// example.php:
// namespace Acme\Example;
// function test_namespaced_function()
// {
// }
include "example.php";

test_namespaced_function()

Attempted to call function 'test_namespaced_function' from namespace 'Foo\Bar\Baz' in foo.php line 12. Did you mean to call: '\acme\example\test_namespaced_function'?

Undefined Function: Unknown Function (Global Namespace)

<?php
test_namespaced_function()

Attempted to call function 'test_namespaced_function' from the global namespace in foo.php line 12.

Undefined Function: Unknown Function (Full Namespace)

<?php
namespace Foo\Bar\Baz;

test_namespaced_function()

Attempted to call function 'test_namespaced_function' from namespace 'Foo\Bar\Baz' in foo.php line 12.

@fabpot
Copy link
Member Author

fabpot commented Jul 24, 2013

The biggest different with the original PR is that there is no hardcoded class map anymore. Instead, we use the well-known autoloaders to find class candidates.

I have also refactored the code to make more flexible and easily testable.

@fabpot
Copy link
Member Author

fabpot commented Jul 24, 2013

This makes #8281 a little bit more difficult as everything added in this PR really only belongs to the dev environement.

@fabpot
Copy link
Member Author

fabpot commented Jul 24, 2013

One proposal (related to what we are doing here -- but can be done in another PR) would be to move the DebugClassLoader class from the ClassLoader component to the Debug component. Of course, we would keep the one in ClassLoader for BC but it would be removed in 3.0.

Beside being more consistent in terms of where the features belongs, it also makes sense as the Symfony ClassLoader component is less used than before as autoloading is now mostly done by Composer.

What do you think about that?

@GromNaN
Copy link
Member

GromNaN commented Jul 24, 2013

How does it works with Interfaces and Traits? Does the message is contextualized?

Edit: It's perfect. fabpot@6671945#L0R270

@fabpot
Copy link
Member Author

fabpot commented Jul 24, 2013

Interfaces and traits are supported and the message is contextualized.

@stof
Copy link
Member

stof commented Jul 24, 2013

+1 for moving the debug class loader to the Debug component. It would also remove the optional dependency from the debug component

*
* @author Fabien Potencier <[email protected]>
*/
class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
Copy link
Member

Choose a reason for hiding this comment

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

I would rename the class to ClassNotFoundHandler to be shorter and avoid repeating the namespace. What do you think ?

Copy link
Member Author

Choose a reason for hiding this comment

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

The way it is named now is how we do it elsewhere, so this is consistent.

fabpot added a commit that referenced this pull request Jul 29, 2013
This PR was merged into the master branch.

Discussion
----------

[Debug] Developer friendly Class Not Found and Undefined Function errors

This is a followup of #8156

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8156
| License       | MIT
| Doc PR        | n/a

Here is the original description from #8156:

Per a discussion with @weaverryan and others, I took a crack at enhancing the exception display for class not found errors and undefined function errors. It is not the cleanest solution but this is a work in progress to see whether or not following this path makes sense.

# Class Names

## Class Not Found: Unknown Class (Global Namespace)

```php
<?php
new WhizBangFactory();
```

> Attempted to load class 'WhizBangFactory' from the global namespace in foo.php line 12. Did you forget a use statement for this class?

## Class Not Found: Unknown Class (Full Namespace)

```php
<?php
namespace Foo\Bar;
new WhizBangFactory();
```

> Attempted to load class 'WhizBangFactory' from namespace 'Foo\Bar' in foo.php line 12. Do you need to 'use' it from another namespace?

## Class Not Found: Well Known Class (Global Namespace)

```php
<?php
new Request();
```

> Attempted to load class 'Request' from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add 'use Symfony\Component\HttpFoundation\Request' at the top of this file?

## Class Not Found: Well Known Class (Full Namespace)

```php
<?php
namespace Foo\Bar;
new Request();
```

> Attempted to load class 'Request' from namespace 'Foo\Bar' in foo.php line 12. Do you need to 'use' it from another namespace? Perhaps you need to add 'use Symfony\Component\HttpFoundation\Request' at the top of this file?

# Functions

## Undefined Function (Global Namespace)

```php
<?php
// example.php:
// namespace Acme\Example;
// function test_namespaced_function()
// {
// }
include "example.php";

test_namespaced_function()
```

> Attempted to call function 'test_namespaced_function' from the global namespace in foo.php line 12. Did you mean to call: '\acme\example\test_namespaced_function'?

## Undefined Function (Full Namespace)

```php
<?php
namespace Foo\Bar\Baz;

// example.php:
// namespace Acme\Example;
// function test_namespaced_function()
// {
// }
include "example.php";

test_namespaced_function()
```

> Attempted to call function 'test_namespaced_function' from namespace 'Foo\Bar\Baz' in foo.php line 12. Did you mean to call: '\acme\example\test_namespaced_function'?

## Undefined Function: Unknown Function (Global Namespace)

```php
<?php
test_namespaced_function()
```

> Attempted to call function 'test_namespaced_function' from the global namespace in foo.php line 12.

## Undefined Function: Unknown Function (Full Namespace)

```php
<?php
namespace Foo\Bar\Baz;

test_namespaced_function()
```

> Attempted to call function 'test_namespaced_function' from namespace 'Foo\Bar\Baz' in foo.php line 12.

Commits
-------

bde67f0 fixed an error message
80e19e2 [Debug] added some missing phpdocs
968764b [Debug] refactored unit tests
cefa1b5 [Debug] moved special fatal error handlers to their own classes
53ab284 [Debug] made Debug find FQCN automatically based on well-known autoloaders
208ca5f [Debug] made guessing of possible class names more flexible
a0b1585 [Debug] fixed CS
6671945 Developer friendly Class Not Found and Undefined Function errors.
@fabpot fabpot closed this Jul 29, 2013
@fabpot fabpot merged commit bde67f0 into symfony:master Jul 29, 2013
fabpot added a commit that referenced this pull request Aug 30, 2013
This PR was merged into the master branch.

Discussion
----------

duplicated the DebugClassLoader in the Debug component

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

This is a follow-up for #8553.

Commits
-------

d146461 duplicated the DebugClassLoader in the Debug component
@fabpot fabpot deleted the class-not-found-exception branch April 27, 2014 09:24
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