Skip to content

Commit b5ac450

Browse files
author
Michał Żarnecki
committed
PHPSTAN refactoring
1 parent 55663a6 commit b5ac450

9 files changed

+34
-10
lines changed

app/src/service/DocumentLoader.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public function __construct(
1515
public function loadDocuments(): void
1616
{
1717
$path = __DIR__.'/../documents';
18-
$files = array_diff(scandir($path), ['.', '..']);
18+
$files = array_diff((array) scandir($path), ['.', '..']);
1919
$total = count($files);
2020

2121
$skipFirstN = 1330; //replace this to skip N documents for debugging
2222
foreach ($files as $index => $file) {
2323
if ($index < $skipFirstN) {
2424
continue;
2525
}
26-
$document = file_get_contents($path.'/'.$file);
26+
$document = (string) file_get_contents($path.'/'.$file);
2727
//cut document to tokens limit 8192
2828
if (str_word_count($document) >= 0.75 * 8000) {
2929
$words = explode(' ', $document);
@@ -34,6 +34,8 @@ public function loadDocuments(): void
3434
$responseDocumentsChunks = $this->textEncoder->getEmbeddings($document);
3535
} catch (\Throwable $e) {
3636
error_log($e->getMessage());
37+
38+
return;
3739
}
3840

3941
foreach ($responseDocumentsChunks as $chunk) {
@@ -46,13 +48,14 @@ public function loadDocuments(): void
4648

4749
private function showProgress(int|string $index, int $total, int $skip): void
4850
{
51+
if (! is_int($index)) {
52+
return;
53+
}
4954
$all = $total - $skip;
5055
$numLoaded = $index - $skip + 1;
5156
$progress = '';
5257
if ($numLoaded % 10 === 0) {
53-
for ($i = 0; $i < $numLoaded / 10; $i++) {
54-
$progress .= '-';
55-
}
58+
$progress = str_repeat('-', $numLoaded / 10);
5659
fwrite(STDOUT, "{$progress}\nLoaded {$numLoaded} of {$all} source documents to vector DB\n");
5760
}
5861
}

app/src/service/DocumentProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ final class DocumentProvider extends AbstractDocumentRepository implements Stage
1111
{
1212
/**
1313
* @param string $distanceFunction l2|cosine|innerProduct
14+
* @return string[][]
1415
*/
1516
public function getSimilarDocuments(
1617
string $prompt,
@@ -40,6 +41,8 @@ public function getSimilarDocuments(
4041

4142
/**
4243
* Sometimes its worth to use efficient algorithm for querying vector DB and more precise to rerank chosen documents in
44+
*
45+
* @return string[][]
4346
*/
4447
public function rerank(string $prompt, array $documents): array
4548
{

app/src/service/RAGPromptProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ final class RAGPromptProvider implements StageInterface
1212
{
1313
public const CONTEXT_TOKEN_COUNT = 5000;
1414

15+
/**
16+
* @param string[] $documents
17+
*/
1518
public function getRAGPrompt(array $documents, string $prompt): string
1619
{
1720
$contextTokenCount = self::CONTEXT_TOKEN_COUNT - TokenizerX::count($prompt) - 20;

app/src/service/TextSplitter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class TextSplitter
66
{
7+
/**
8+
* @return string[]
9+
*/
710
public function splitDocumentIntoChunks(string $document, int $chunkSize, int $overlap): array
811
{
912
$chunks = [];

app/src/service/claude/AbstractClaudeAPIClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function __construct()
2929
]);
3030
}
3131

32+
/**
33+
* @return string[]
34+
*
35+
* @throws \JsonException
36+
*/
3237
protected function request(string $prompt, string $endpoint, string $model): array
3338
{
3439
$url = $endpoint;

app/src/service/claude/GeneratedTextFromClaudeProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use League\Pipeline\StageInterface;
66
use service\GeneratedTextProviderInterface;
7+
use service\pipeline\Payload;
78

89
final class GeneratedTextFromClaudeProvider extends AbstractClaudeAPIClient implements GeneratedTextProviderInterface, StageInterface
910
{
@@ -23,9 +24,8 @@ public function generateText(string $prompt, string $sourceDocuments): string
2324

2425
/**
2526
* @param Payload $payload
26-
* @return string
2727
*/
28-
public function __invoke($payload)
28+
public function __invoke($payload): string
2929
{
3030
return $this->generateText($payload->getPrompt(), $payload->getRagPrompt());
3131
}

app/src/service/deepseek/AbstractDeepSeekAPIClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public function __construct()
2626
]);
2727
}
2828

29+
/**
30+
* @param string[] $messages
31+
* @param string[] $options
32+
* @return string[]
33+
*
34+
* @throws \JsonException
35+
*/
2936
protected function request(array $messages, string $model, bool $stream = false, array $options = []): array
3037
{
3138
$data = array_merge([

app/src/service/evaluate/StringComparisonEvaluator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class StringComparisonEvaluator
66
{
7-
public function calculateBLEU(string $reference, string $candidate, $n = 1): float
7+
public function calculateBLEU(string $reference, string $candidate, int $n = 1): float
88
{
99
$candidateWords = explode(' ', $candidate);
1010
$referenceWords = explode(' ', $reference);
@@ -22,7 +22,7 @@ public function calculateBLEU(string $reference, string $candidate, $n = 1): flo
2222
$matches++;
2323
}
2424
}
25-
$nGramMatches[$i] = $matches / max(is_countable($candidateNGrams) ? count($candidateNGrams) : 0, 1);
25+
$nGramMatches[$i] = $matches / max((is_countable($candidateNGrams) ? count($candidateNGrams) : 0), 1);
2626
}
2727

2828
$precision = array_product($nGramMatches);

app/src/test/TextSplitterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function testTextSplitter_keepsCommonPartBetweenChunks(): void
1313
{
1414
$textSplitter = new TextSplitter();
1515
$chunks = $textSplitter->splitDocumentIntoChunks($this->getLoremIpsum(), 300, 100);
16-
$this->assertEquals(substr((string) $chunks[0], strlen((string) $chunks[0]) - 100), substr((string) $chunks[1], 0, 100));
16+
$this->assertEquals(substr($chunks[0], strlen($chunks[0]) - 100), substr($chunks[1], 0, 100));
1717
}
1818

1919
public function testTextSplitter_splitsToExpectedNumberOfChunks(): void

0 commit comments

Comments
 (0)