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

ErrorException: Uninitialized string offset 1 #1016

Closed
fuale opened this issue Dec 7, 2021 · 11 comments
Closed

ErrorException: Uninitialized string offset 1 #1016

fuale opened this issue Dec 7, 2021 · 11 comments

Comments

@fuale
Copy link

fuale commented Dec 7, 2021

at 4.0.1

  at vendor/zircote/swagger-php/src/Analysers/TokenScanner.php:128
    124▕                             // no function body
    125▕                             $this->skipTo($tokens, ';');
    126▕                         }
    127▕ 
  ➜ 128▕                         $units[$currentName]['methods'][] = $token[1];
    129▕                     }
    130▕                     break;
    131▕                 case T_VARIABLE:
    132▕                     if (1 == count($stack) && $currentName) {

Place where i found bug using debugger

$worklogs->issues = array_map(static function ($issue) use ($client) {
    if ($issue->fields->worklog->total > $issue->fields->worklog->maxResults) {
        $issue->fields->worklog = JiraQuery::getWorklogs($client, $issue->key);
    }

    return $issue;
}, $worklogs->issues);

return collect($worklogs->issues);

I Do something wrong?

@fuale
Copy link
Author

fuale commented Dec 7, 2021

I think it's a problem with nested static function statement

@fuale
Copy link
Author

fuale commented Dec 7, 2021

additional debug info

$lastToken = {array} [3]
 0 = {int} 321
 1 = "static"
 2 = {int} 64
$namespace = "App\Services\Jira"
$stack = {array} [1]
 0 = "{"
$token = "("

@fuale
Copy link
Author

fuale commented Dec 7, 2021

Yes, I commented out this piece of code, and now it's working

@DerManoMann
Copy link
Collaborator

Sigh, thanks. Another test case, another release :/

@DerManoMann
Copy link
Collaborator

Hmm, there must be something else. I've added this methot to one of the test fixtures and that works fine.

    public function whatever($client)
    {
        $worklogs = (object)[];
        $worklogs->issues = array_map(static function ($issue) use ($client) {
            if ($issue->fields->worklog->total > $issue->fields->worklog->maxResults) {
                $issue->fields->worklog = 'foo';
            }

            return $issue;
        }, $worklogs->issues);

        return collect($worklogs->issues);
    }

@fuale Would you be able to extract a code snippet into a separate file or something that does break the code and share that? There must be some more context that is missing to make it go wrong.

@DerManoMann
Copy link
Collaborator

DerManoMann commented Dec 7, 2021

You can see the code I used for a fixture here:

https://github.com/DerManoMann/swagger-php/blob/more-nested-functions/tests/Fixtures/PHP/AnonymousFunctions.php#L56

If you feel like it you could also try to change it to make the test break :)

@fuale
Copy link
Author

fuale commented Dec 7, 2021

oh, my fault

public function whatever($client)
{
    foreach ($prepareParams as $key => $value) {
        $jql = preg_replace("/:${key}/", $value, $jql);
    }

    $worklogs = JiraQuery::runQuery($client, $jql, [], ['worklogs']);

    $worklogs->issues = array_map(static function ($issue) use ($client) {
        if ($issue->fields->worklog->total > $issue->fields->worklog->maxResults) {
            $issue->fields->worklog = JiraQuery::getWorklogs($client, $issue->key);
        }

        return $issue;
    }, $worklogs->issues);

    return collect($worklogs->issues);
}

gives me

➜ ./bin/phpunit --filter TokenScannerTest
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

.........E                                                        10 / 10 (100%)

Time: 00:00.008, Memory: 10.00 MB

There was 1 error:

1) OpenApi\Tests\Analysers\TokenScannerTest::testScanFile with data set "AnonymousFunctions" ('PHP/AnonymousFunctions.php', array(array(array('OpenApi\Annotations\Info'), array(), array(), array('index', 'query', 'other', 'shortFn', 'withUse', 'whatever'), array())))
Uninitialized string offset 1

/home/w/Projects/swagger-php/src/Analysers/TokenScanner.php:128
/home/w/Projects/swagger-php/src/Analysers/TokenScanner.php:21
/home/w/Projects/swagger-php/tests/Analysers/TokenScannerTest.php:182

ERRORS!
Tests: 10, Assertions: 19, Errors: 1.

@fuale
Copy link
Author

fuale commented Dec 7, 2021

@DerManoMann this should help
It seems that the problem with preg_replace

@DerManoMann
Copy link
Collaborator

Stripped it down to this - you really need bot the preg_replace and the array_map! Now for the fun part...

    public function whatever(string $key = 'xx')
    {
        preg_replace("/:${key}/", 'y', 'abx');

        array_map(static function ($issue) use ($key) {
            return $issue;
        }, []);
    }

@DerManoMann
Copy link
Collaborator

There is always one more token to consider... Updated PR #1017 ready for some testing @fuale .

@DerManoMann
Copy link
Collaborator

Fixed in 4.0.2

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

No branches or pull requests

2 participants