3232use FormatPHP \Extractor \Parser \Descriptor \PhpParser ;
3333use FormatPHP \Extractor \Parser \DescriptorParserInterface ;
3434use FormatPHP \Extractor \Parser \ParserErrorCollection ;
35+ use FormatPHP \Format \WriterInterface ;
36+ use FormatPHP \Format \WriterOptions ;
3537use FormatPHP \Util \FileSystemHelper ;
3638use FormatPHP \Util \FormatHelper ;
3739use FormatPHP \Util \Globber ;
3840use LogicException ;
3941use Psr \Log \LoggerInterface ;
42+ use Ramsey \Collection \Exception \CollectionMismatchException ;
4043
4144use function class_exists ;
4245use function count ;
4750
4851/**
4952 * Extracts message descriptors from application source code
53+ *
54+ * @psalm-import-type DescriptorParserCallable from DescriptorParserInterface
55+ * @psalm-import-type WriterType from WriterInterface
5056 */
5157class MessageExtractor
5258{
@@ -82,6 +88,7 @@ public function __construct(
8288 * @throws InvalidArgumentException
8389 * @throws ImproperContextException
8490 * @throws LogicException
91+ * @throws CollectionMismatchException
8592 */
8693 public function process (array $ files ): void
8794 {
@@ -129,6 +136,7 @@ public function getErrors(): ParserErrorCollection
129136 * @throws UnableToProcessFileException
130137 * @throws ImproperContextException
131138 * @throws LogicException
139+ * @throws CollectionMismatchException
132140 */
133141 private function parse (DescriptorCollection $ descriptors , string $ filePath ): DescriptorCollection
134142 {
@@ -163,15 +171,14 @@ private function getDescriptorParsers(): array
163171 */
164172 private function loadDescriptorParser (string $ parserNameOrScript ): DescriptorParserInterface
165173 {
166- switch (strtolower ($ parserNameOrScript )) {
167- case 'php ' :
168- return new PhpParser ($ this ->file );
174+ if (strtolower ($ parserNameOrScript ) === 'php ' ) {
175+ return new PhpParser ($ this ->file );
169176 }
170177
171178 if (class_exists ($ parserNameOrScript ) && is_a ($ parserNameOrScript , DescriptorParserInterface::class, true )) {
172179 $ parser = new $ parserNameOrScript ();
173180 } else {
174- /** @var Closure(string,MessageExtractorOptions,ParserErrorCollection):DescriptorCollection | null $parser */
181+ /** @var DescriptorParserCallable | null $parser */
175182 $ parser = $ this ->file ->loadClosureFromScript ($ parserNameOrScript );
176183 }
177184
@@ -194,7 +201,9 @@ private function loadDescriptorParser(string $parserNameOrScript): DescriptorPar
194201 }
195202
196203 /**
197- * @param callable(DescriptorCollection,MessageExtractorOptions):array<mixed> $formatter
204+ * @see WriterInterface
205+ *
206+ * @param WriterType $formatter
198207 *
199208 * @throws UnableToWriteFileException
200209 * @throws InvalidArgumentException
@@ -203,7 +212,11 @@ private function write(callable $formatter, DescriptorCollection $descriptors):
203212 {
204213 $ file = $ this ->options ->outFile ?? 'php://output ' ;
205214
206- $ messages = $ formatter ($ descriptors , $ this ->options );
215+ $ writerOptions = new WriterOptions ();
216+ $ writerOptions ->includesSourceLocation = $ this ->options ->extractSourceLocation ;
217+ $ writerOptions ->includesPragma = $ this ->options ->pragma !== null ;
218+
219+ $ messages = $ formatter ($ descriptors , $ writerOptions );
207220 if (count ($ messages ) === 0 ) {
208221 $ messages = (object ) $ messages ;
209222 }
0 commit comments