Skip to content

Commit

Permalink
ci: add rector/rector (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored Sep 22, 2024
1 parent bc89175 commit c1fad8a
Show file tree
Hide file tree
Showing 29 changed files with 147 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
- name: Code Style PHP
run: vendor/bin/php-cs-fixer fix --dry-run

- name: Rector
run: vendor/bin/rector

- name: PHPStan
run: vendor/bin/phpstan analyse

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ qa:
vendor/bin/php-cs-fixer fix
vendor/bin/phpstan
vendor/bin/phpunit
vendor/bin/rector

qa-lowest:
composer update --prefer-lowest
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^11.3",
"probots-io/pinecone-php": "^1.0",
"rector/rector": "^1.2",
"symfony/clock": "^6.4 || ^7.1",
"symfony/console": "^6.4 || ^7.1",
"symfony/css-selector": "^6.4 || ^7.1",
Expand Down
24 changes: 24 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\PHPUnit\Set\PHPUnitSetList;

return RectorConfig::configure()
->withPaths([
__DIR__.'/examples',
__DIR__.'/src',
__DIR__.'/tests',
])
->withPhpSets(php82: true)
->withSets([
PHPUnitSetList::PHPUNIT_110,
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
])
->withImportNames(importNames: true, importShortClasses: false)
->withSkip([
ClosureToArrowFunctionRector::class,
])
->withTypeCoverageLevel(0);
2 changes: 1 addition & 1 deletion src/Anthropic/Model/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpLlm\LlmChain\Response\Choice;
use PhpLlm\LlmChain\Response\Response;

final class Claude implements LanguageModel
final readonly class Claude implements LanguageModel
{
public function __construct(
private ClaudeRuntime $runtime,
Expand Down
2 changes: 1 addition & 1 deletion src/Anthropic/Runtime/Anthropic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpLlm\LlmChain\Anthropic\ClaudeRuntime;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class Anthropic implements ClaudeRuntime
final readonly class Anthropic implements ClaudeRuntime
{
public function __construct(
private HttpClientInterface $httpClient,
Expand Down
2 changes: 1 addition & 1 deletion src/DocumentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpLlm\LlmChain\Document\Document;
use PhpLlm\LlmChain\Store\StoreInterface;

final class DocumentEmbedder
final readonly class DocumentEmbedder
{
public function __construct(
private EmbeddingModel $embeddings,
Expand Down
2 changes: 1 addition & 1 deletion src/OpenAI/Model/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpLlm\LlmChain\OpenAI\Model\Embeddings\Version;
use PhpLlm\LlmChain\OpenAI\Runtime;

final class Embeddings implements EmbeddingModel
final readonly class Embeddings implements EmbeddingModel
{
public function __construct(
private Runtime $runtime,
Expand Down
2 changes: 1 addition & 1 deletion src/OpenAI/Model/Gpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpLlm\LlmChain\Response\Response;
use PhpLlm\LlmChain\Response\ToolCall;

final class Gpt implements LanguageModel
final readonly class Gpt implements LanguageModel
{
public function __construct(
private Runtime $runtime,
Expand Down
10 changes: 5 additions & 5 deletions src/OpenAI/Runtime/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
final class Azure extends AbstractRuntime implements Runtime
{
public function __construct(
private HttpClientInterface $httpClient,
private string $baseUrl,
private string $deployment,
private string $apiVersion,
private string $key,
private readonly HttpClientInterface $httpClient,
private readonly string $baseUrl,
private readonly string $deployment,
private readonly string $apiVersion,
private readonly string $key,
) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/OpenAI/Runtime/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
final class OpenAI extends AbstractRuntime implements Runtime
{
public function __construct(
private HttpClientInterface $httpClient,
private string $apiKey,
private readonly HttpClientInterface $httpClient,
private readonly string $apiKey,
) {
}

Expand Down
12 changes: 6 additions & 6 deletions src/Store/Azure/SearchStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class SearchStore implements VectorStoreInterface
final readonly class SearchStore implements VectorStoreInterface
{
public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly string $endpointUrl,
private readonly string $apiKey,
private readonly string $indexName,
private readonly string $apiVersion,
private HttpClientInterface $httpClient,
private string $endpointUrl,
private string $apiKey,
private string $indexName,
private string $apiVersion,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Store/ChromaDb/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

final class Store implements VectorStoreInterface
final readonly class Store implements VectorStoreInterface
{
public function __construct(
private Client $client,
Expand Down
4 changes: 2 additions & 2 deletions src/StructuredOutput/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type;

final class SchemaFactory
final readonly class SchemaFactory
{
public function __construct(
private readonly PropertyInfoExtractor $propertyInfo,
private PropertyInfoExtractor $propertyInfo,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/ToolBox/AsTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace PhpLlm\LlmChain\ToolBox;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AsTool
final readonly class AsTool
{
public function __construct(
public readonly string $name,
public readonly string $description,
public readonly string $method = '__invoke',
public string $name,
public string $description,
public string $method = '__invoke',
) {
}
}
12 changes: 6 additions & 6 deletions src/ToolBox/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
/**
* @phpstan-import-type ParameterDefinition from ParameterAnalyzer
*/
final class Metadata implements \JsonSerializable
final readonly class Metadata implements \JsonSerializable
{
/**
* @param ParameterDefinition|null $parameters
*/
public function __construct(
public readonly string $className,
public readonly string $name,
public readonly string $description,
public readonly string $method,
public readonly ?array $parameters,
public string $className,
public string $name,
public string $description,
public string $method,
public ?array $parameters,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/Tool/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Clock\ClockInterface;

#[AsTool('clock', description: 'Provides the current date and time.')]
final class Clock
final readonly class Clock
{
public function __construct(
private ClockInterface $clock,
Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/Tool/SerpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsTool(name: 'serpapi', description: 'search for information on the internet')]
final class SerpApi
final readonly class SerpApi
{
public function __construct(
private HttpClientInterface $httpClient,
Expand Down
2 changes: 1 addition & 1 deletion src/ToolBox/Tool/Wikipedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#[AsTool('wikipedia_search', description: 'Searches Wikipedia for a given query', method: 'search')]
#[AsTool('wikipedia_article', description: 'Retrieves a Wikipedia article by its title', method: 'getArticle')]
final class Wikipedia
final readonly class Wikipedia
{
public function __construct(
private HttpClientInterface $httpClient,
Expand Down
4 changes: 2 additions & 2 deletions src/ToolBox/ToolAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use PhpLlm\LlmChain\Exception\InvalidToolImplementation;

final class ToolAnalyzer
final readonly class ToolAnalyzer
{
public function __construct(
private readonly ParameterAnalyzer $parameterAnalyzer = new ParameterAnalyzer(),
private ParameterAnalyzer $parameterAnalyzer = new ParameterAnalyzer(),
) {
}

Expand Down
19 changes: 13 additions & 6 deletions tests/Message/MessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Message\MessageBag;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -16,7 +17,8 @@
#[Small]
final class MessageBagTest extends TestCase
{
public function testGetSystemMessage(): void
#[Test]
public function getSystemMessage(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -29,7 +31,8 @@ public function testGetSystemMessage(): void
self::assertSame('My amazing system prompt.', $systemMessage->content);
}

public function testGetSystemMessageWithoutSystemMessage(): void
#[Test]
public function getSystemMessageWithoutSystemMessage(): void
{
$messageBag = new MessageBag(
Message::ofAssistant('It is time to sleep.'),
Expand All @@ -39,7 +42,8 @@ public function testGetSystemMessageWithoutSystemMessage(): void
self::assertNull($messageBag->getSystemMessage());
}

public function testWith(): void
#[Test]
public function with(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -55,7 +59,8 @@ public function testWith(): void
self::assertSame('It is time to wake up.', $newMessageBag[3]->content);
}

public function testWithoutSystemMessage(): void
#[Test]
public function withoutSystemMessage(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand All @@ -70,7 +75,8 @@ public function testWithoutSystemMessage(): void
self::assertSame('It is time to sleep.', $newMessageBag[0]->content);
}

public function testPrepend(): void
#[Test]
public function prepend(): void
{
$messageBag = new MessageBag(
Message::ofAssistant('It is time to sleep.'),
Expand All @@ -85,7 +91,8 @@ public function testPrepend(): void
self::assertSame('My amazing system prompt.', $newMessageBag[0]->content);
}

public function testJsonSerialize(): void
#[Test]
public function jsonSerialize(): void
{
$messageBag = new MessageBag(
Message::forSystem('My amazing system prompt.'),
Expand Down
19 changes: 13 additions & 6 deletions tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -17,7 +18,8 @@
#[Small]
final class MessageTest extends TestCase
{
public function testCreateSystemMessage(): void
#[Test]
public function createSystemMessage(): void
{
$message = Message::forSystem('My amazing system prompt.');

Expand All @@ -29,7 +31,8 @@ public function testCreateSystemMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateAssistantMessage(): void
#[Test]
public function createAssistantMessage(): void
{
$message = Message::ofAssistant('It is time to sleep.');

Expand All @@ -41,7 +44,8 @@ public function testCreateAssistantMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateAssistantMessageWithToolCalls(): void
#[Test]
public function createAssistantMessageWithToolCalls(): void
{
$toolCalls = [
new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']),
Expand All @@ -57,7 +61,8 @@ public function testCreateAssistantMessageWithToolCalls(): void
self::assertTrue($message->hasToolCalls());
}

public function testCreateUserMessage(): void
#[Test]
public function createUserMessage(): void
{
$message = Message::ofUser('Hi, my name is John.');

Expand All @@ -69,7 +74,8 @@ public function testCreateUserMessage(): void
self::assertFalse($message->hasToolCalls());
}

public function testCreateToolCallMessage(): void
#[Test]
public function createToolCallMessage(): void
{
$toolCall = new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']);
$message = Message::ofToolCall($toolCall, 'Foo bar.');
Expand All @@ -84,7 +90,8 @@ public function testCreateToolCallMessage(): void
}

#[DataProvider('provideJsonScenarios')]
public function testJsonSerialize(Message $message, array $expected): void
#[Test]
public function jsonSerialize(Message $message, array $expected): void
{
self::assertSame($expected, $message->jsonSerialize());
}
Expand Down
Loading

0 comments on commit c1fad8a

Please sign in to comment.