Skip to content

Commit 6bfb1ed

Browse files
OskarStarkclaude
andcommitted
Fix PHPStan errors and apply code style improvements
- Fix method call from vectorize() to vectorizeTextDocuments() in Indexer - Change null|string to ?string type hints in loader interfaces - Remove unused imports from ReplaceTextTransformer - Apply Yoda conditions in tests - Use dedicated InvalidArgumentException for TextFileLoader null source - Update parameter documentation for better clarity - Remove trailing spaces and apply consistent formatting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 05e6b69 commit 6bfb1ed

File tree

9 files changed

+42
-50
lines changed

9 files changed

+42
-50
lines changed

demo/src/Blog/Embedder.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@
1111

1212
namespace App\Blog;
1313

14+
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
1415
use Symfony\AI\Store\Document\Metadata;
1516
use Symfony\AI\Store\Document\TextDocument;
16-
use Symfony\AI\Store\IndexerInterface;
17+
use Symfony\AI\Store\Document\TransformerInterface;
18+
use Symfony\AI\Store\Document\VectorizerInterface;
19+
use Symfony\AI\Store\Indexer;
20+
use Symfony\AI\Store\StoreInterface;
1721

1822
final readonly class Embedder
1923
{
24+
/**
25+
* @param TransformerInterface[] $transformers
26+
*/
2027
public function __construct(
2128
private FeedLoader $loader,
22-
private IndexerInterface $indexer,
29+
private array $transformers,
30+
private VectorizerInterface $vectorizer,
31+
private StoreInterface $store,
2332
) {
2433
}
2534

@@ -30,6 +39,16 @@ public function embedBlog(): void
3039
$documents[] = new TextDocument($post->id, $post->toString(), new Metadata($post->toArray()));
3140
}
3241

33-
$this->indexer->index($documents);
42+
// Use the new Indexer with InMemoryLoader for the complete pipeline
43+
$inMemoryLoader = new InMemoryLoader($documents);
44+
$indexer = new Indexer(
45+
$inMemoryLoader,
46+
$this->transformers,
47+
$this->vectorizer,
48+
$this->store
49+
);
50+
51+
// Process through the complete pipeline: load → transform → vectorize → store
52+
$indexer->index(); // Uses default null source, perfect for InMemoryLoader
3453
}
3554
}

examples/indexer/index-inmemory-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
$documents = [
2929
new TextDocument(
30-
Uuid::v4(),
30+
Uuid::v4(),
3131
'Artificial Intelligence is transforming the way we work and live. Machine learning algorithms can now process vast amounts of data and make predictions with remarkable accuracy.',
3232
new Metadata(['title' => 'AI Revolution'])
3333
),

src/store/src/Document/Loader/InMemoryLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
) {
3131
}
3232

33-
public function load(null|string $source, array $options = []): iterable
33+
public function load(?string $source, array $options = []): iterable
3434
{
3535
yield from $this->documents;
3636
}

src/store/src/Document/Loader/TextFileLoader.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\AI\Store\Document\LoaderInterface;
1515
use Symfony\AI\Store\Document\Metadata;
1616
use Symfony\AI\Store\Document\TextDocument;
17+
use Symfony\AI\Store\Exception\InvalidArgumentException;
1718
use Symfony\AI\Store\Exception\RuntimeException;
1819
use Symfony\Component\Uid\Uuid;
1920

@@ -22,10 +23,14 @@
2223
*/
2324
final readonly class TextFileLoader implements LoaderInterface
2425
{
25-
public function load(null|string $source, array $options = []): iterable
26+
public function load(?string $source, array $options = []): iterable
2627
{
27-
if (null === $source || !is_file($source)) {
28-
throw new RuntimeException(\sprintf('File "%s" does not exist.', $source ?? 'null'));
28+
if (null === $source) {
29+
throw new InvalidArgumentException('TextFileLoader requires a file path as source, null given.');
30+
}
31+
32+
if (!is_file($source)) {
33+
throw new RuntimeException(\sprintf('File "%s" does not exist.', $source));
2934
}
3035

3136
$content = file_get_contents($source);

src/store/src/Document/LoaderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
interface LoaderInterface
1818
{
1919
/**
20-
* @param null|string $source Identifier for the loader to load the documents from, e.g. file path, folder, or URL. Can be null for InMemoryLoader.
20+
* @param string|null $source Identifier for the loader to load the documents from, e.g. file path, folder, or URL. Can be null for InMemoryLoader.
2121
* @param array<string, mixed> $options loader specific set of options to control the loading process
2222
*
2323
* @return iterable<TextDocument> iterable of TextDocuments loaded from the source
2424
*/
25-
public function load(null|string $source, array $options = []): iterable;
25+
public function load(?string $source, array $options = []): iterable;
2626
}

src/store/src/Document/Transformer/ReplaceTextTransformer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111

1212
namespace Symfony\AI\Store\Document\Transformer;
1313

14-
use Symfony\AI\Store\Document\Metadata;
15-
use Symfony\AI\Store\Document\TextDocument;
1614
use Symfony\AI\Store\Document\TransformerInterface;
1715
use Symfony\AI\Store\Exception\InvalidArgumentException;
18-
use Symfony\Component\Uid\Uuid;
1916

2017
/**
2118
* Replaces specified text within document content.

src/store/src/Indexer.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,7 @@ public function __construct(
3636
}
3737

3838
/**
39-
* @param TextDocument|iterable<TextDocument> $documents
40-
* @param int $chunkSize number of documents to vectorize and store in one batch
41-
*/
42-
public function index(TextDocument|iterable $documents, int $chunkSize = 50): void
43-
{
44-
if ($documents instanceof TextDocument) {
45-
$documents = [$documents];
46-
}
47-
48-
$counter = 0;
49-
$chunk = [];
50-
foreach ($documents as $document) {
51-
$chunk[] = $document;
52-
++$counter;
53-
54-
if ($chunkSize === \count($chunk)) {
55-
$this->store->add(...$this->vectorizer->vectorize($chunk));
56-
$chunk = [];
57-
}
58-
}
59-
60-
if (\count($chunk) > 0) {
61-
$this->store->add(...$this->vectorizer->vectorize($chunk));
62-
}
63-
64-
$this->logger->debug(0 === $counter ? 'No documents to index' : \sprintf('Indexed %d documents', $counter));
65-
}
66-
67-
/**
68-
* Process sources through the complete document pipeline: load → transform → vectorize → store.
69-
*
70-
* @param null|string|array<string> $source Source identifier (file path, URL, etc.), array of sources, or null for InMemoryLoader
39+
* @param null|string|array<string> $source Source identifier (file path, URL, etc.), array of sources, or null if the loader does not need a source, e.g. InMemoryLoader
7140
* @param array<string, mixed> $options Processing options
7241
*/
7342
public function index(null|string|array $source = null, array $options = []): void
@@ -103,13 +72,13 @@ public function index(null|string|array $source = null, array $options = []): vo
10372
++$counter;
10473

10574
if ($chunkSize === \count($chunk)) {
106-
$this->store->add(...$this->vectorizer->vectorize($chunk));
75+
$this->store->add(...$this->vectorizer->vectorizeTextDocuments($chunk));
10776
$chunk = [];
10877
}
10978
}
11079

11180
if (\count($chunk) > 0) {
112-
$this->store->add(...$this->vectorizer->vectorize($chunk));
81+
$this->store->add(...$this->vectorizer->vectorizeTextDocuments($chunk));
11382
}
11483

11584
$this->logger->debug('Document processing completed', [

src/store/src/IndexerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
interface IndexerInterface
2020
{
2121
/**
22-
* @param null|string|array<string> $source Source identifier (file path, URL, etc.), array of sources, or null for InMemoryLoader
22+
* Process sources through the complete document pipeline: load → transform → vectorize → store.
23+
*
24+
* @param null|string|array<string> $source Source identifier (file path, URL, etc.), array of sources, or null if the loader does not need a source, e.g. InMemoryLoader
2325
* @param array<string, mixed> $options Processing options
2426
*/
2527
public function index(null|string|array $source = null, array $options = []): void;

src/store/tests/IndexerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public function testIndexEmptyDocumentList()
6969
$logger->expects($this->exactly(2))->method('debug')
7070
->willReturnCallback(function ($message, $context) {
7171
static $callCount = 0;
72-
if ($callCount === 0) {
72+
if (0 === $callCount) {
7373
$this->assertSame('Starting document processing', $message);
7474
$this->assertSame(['source' => null, 'options' => []], $context);
75-
} elseif ($callCount === 1) {
75+
} elseif (1 === $callCount) {
7676
$this->assertSame('Document processing completed', $message);
7777
$this->assertSame(['source' => null, 'documents_processed' => 0], $context);
7878
}

0 commit comments

Comments
 (0)