From 51dd5af81e8c3057f188ed05efb6b28e2441a769 Mon Sep 17 00:00:00 2001 From: Nicholas J Date: Wed, 25 Feb 2015 16:38:17 +0000 Subject: [PATCH] This bugfix adds a truthiness check in the comment section of MessageGenerator. Given the following message excerpt from rethinkdb q2.proto: ```protobuf // A [Term] is either a piece of data (see **Datum** above), or an operator and // its operands. If you have a [Datum], it's stored in the member [datum]. If // you have an operator, its positional arguments are stored in [args] and its // optional arguments are stored in [optargs]. // // A note about type signatures: ``` The MessageGenerator would build the following array ```php array ( 0 => 'A [Term] is either a piece of data (see **Datum** above), or an operator and', 1 => ' its operands. If you have a [Datum], it\'s stored in the member [datum]. If', 2 => ' you have an operator, its positional arguments are stored in [args] and its', 3 => ' optional arguments are stored in [optargs].', 4 => '', 5 => ' A note about type signatures:', ``` When attempting to check and remove a leading space it would throw the following notice for empty strings: ``` PHP Notice: Uninitialized string offset: 0 in /home/vagrant/protoc-gen-php/src/protocolbuffers/generator/php/MessageGenerator.php on line 546 PHP Stack trace: PHP 1. {main}() /home/vagrant/protoc-gen-php/bin/protoc-gen-php:0 PHP 2. Symfony\Component\Console\Application->run() /home/vagrant/protoc-gen-php/bin/protoc-gen-php:31 PHP 3. Symfony\Component\Console\Application->doRun() /home/vagrant/protoc-gen-php/vendor/symfony/console/Symfony/Component/Console/Application.php:126 PHP 4. Symfony\Component\Console\Application->doRunCommand() /home/vagrant/protoc-gen-php/vendor/symfony/console/Symfony/Component/Console/Application.php:195 PHP 5. Symfony\Component\Console\Command\Command->run() /home/vagrant/protoc-gen-php/vendor/symfony/console/Symfony/Component/Console/Application.php:874 PHP 6. protocolbuffers\console\command\GenerateCommand->execute() /home/vagrant/protoc-gen-php/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:253 PHP 7. protocolbuffers\Compiler->compile() /home/vagrant/protoc-gen-php/src/protocolbuffers/console/command/GenerateCommand.php:29 PHP 8. protocolbuffers\generator\php\Generator->generate() /home/vagrant/protoc-gen-php/src/protocolbuffers/Compiler.php:204 PHP 9. protocolbuffers\generator\php\FileGenerator->generateSiblings() /home/vagrant/protoc-gen-php/src/protocolbuffers/generator/php/Generator.php:68 PHP 10. protocolbuffers\generator\php\MessageGenerator->generate() /home/vagrant/protoc-gen-php/src/protocolbuffers/generator/php/FileGenerator.php:168 ``` --- src/protocolbuffers/generator/php/MessageGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocolbuffers/generator/php/MessageGenerator.php b/src/protocolbuffers/generator/php/MessageGenerator.php index 8f144e7..fba2cfd 100644 --- a/src/protocolbuffers/generator/php/MessageGenerator.php +++ b/src/protocolbuffers/generator/php/MessageGenerator.php @@ -543,7 +543,7 @@ public function generate(Printer $printer) if ($dict->getLeadingComments()) { $lines = preg_split("/\r?\n/", trim($dict->getLeadingComments())); foreach ($lines as $line) { - if ($line[0] == " ") { + if ($line && $line[0] == " ") { $line = substr($line, 1); } $printer->put(" * `comment`\n", "comment", $line);