Skip to content
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

php7.4 getCallerFromStackTrace not returning an array causes ErrorException: Trying to access array offset on value of type null #804

Closed
eric-reichenbach opened this issue Jan 2, 2020 · 5 comments
Labels

Comments

@eric-reichenbach
Copy link

  • Telescope Version: 2.1.3
  • Laravel Version: 6.9.0
  • PHP Version: 7.4.1
  • Database Driver & Version:

Description:

if getCallerFromStackTrace doesn't return an array, QueryWatcher.php at line 46 will throw an exception :

ErrorException: Trying to access array offset on value of type null

https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.non-array-access

Steps To Reproduce:

Any scenario in which getCallerFromStackTrace can't find a caller. In my case I ran a test contained in a vendor component that had to write to a test database.

@driesvints driesvints added the bug label Jan 2, 2020
@driesvints
Copy link
Member

Can we create a regression test for this?

@bandgeekndb
Copy link

Trying to dig into this, hoping I'm on the right track. When running a command such as php artisan migrate, the artisan file appears in the stack trace as the first non-vendor file, which is what getCallerFromStackTrace() is looking for.

But, for tests that use the RefreshDatabase trait, all of that work comes from code in the vendor directory, which makes getCallerFromStackTrace() return null (since it finds no code outside the vendor directory in the stack trace), and that causes PHP 7.4 to throw an error when we attempt to access that null value as an array in QueryWatcher.php.

$caller = $this->getCallerFromStackTrace();  //null when RefreshDatabase trait is doing work

Telescope::recordQuery(IncomingEntry::make([
	'connection' => $event->connectionName,
	'bindings' => [],
	'sql' => $this->replaceBindings($event),
	'time' => number_format($time, 2),
	'slow' => isset($this->options['slow']) && $time >= $this->options['slow'],
	'file' => $caller['file'],  //This line is what PHP 7.4 balks at
	'line' => $caller['line'],
	'hash' => $this->familyHash($event),
])->tags($this->tags($event)));

Hoping that helps someone else write the regression test - I'm taking a stab at it, but this isn't my strong suit by any means, hoping someone with sharper skills can come in and save the day here!

@bandgeekndb
Copy link

The reason this doesn't crop up in Telescope's existing tests is because all test cases extend the FeatureTestCase and that class has its own setUp() method. As a result, that FeatureTestCase class (outside the vendor directory) is in the stack trace, so the getCallerFromStackTrace() method doesn't return null.

I was able to alleviate this issue in my own repository by simply adding the following code to Laravel's base TestCase class it provides in the tests/ directory:

protected function setUp(): void
{
    parent::setUp();
}

That ensures that my base test case, outside the vendor directory, will always be in the stack trace, so the issue never comes up.

Beyond a regression test, not sure what the best approach is to fixing this, but I figured I'd share what I've been finding as I dig into this.

@TiBianMod
Copy link

on phpunit.xml file add...

<server name="TELESCOPE_ENABLED" value="false"/>

after that all tests will run correct :)

@driesvints
Copy link
Member

I didn't realise that you were doing this in a testing environment before. Please disable Telescope in your test suite as explained above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants