Skip to content

Commit 205ab97

Browse files
feat: ensure ->toArray() benefits from structural typing
1 parent d79ffc9 commit 205ab97

26 files changed

+210
-8
lines changed

src/Core/Concerns/SdkModel.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
/**
1616
* @internal
17+
*
18+
* @template-covariant Data of array<string, mixed>
1719
*/
1820
trait SdkModel
1921
{
@@ -65,13 +67,13 @@ public function __toString(): string
6567
}
6668

6769
/**
70+
* @internal
71+
*
6872
* Magic get is intended to occur when we have manually unset
6973
* a native class property, indicating an omitted value,
70-
* or a property overridden with an incongruent type.
74+
* or a property overridden with an incongruent type
7175
*
7276
* @throws \Exception
73-
*
74-
* @internal
7577
*/
7678
public function __get(string $key): mixed
7779
{
@@ -92,10 +94,12 @@ public function __get(string $key): mixed
9294
return null;
9395
}
9496

95-
/** @return array<string, mixed> */
97+
/**
98+
* @return Data
99+
*/
96100
public function toArray(): array
97101
{
98-
return $this->__serialize();
102+
return $this->__serialize(); // @phpstan-ignore-line
99103
}
100104

101105
/**
@@ -200,8 +204,10 @@ public function jsonSerialize(): array
200204

201205
/**
202206
* @internal
207+
*
208+
* @param array<string, mixed> $data
203209
*/
204-
public static function fromArray(mixed $data): self
210+
public static function fromArray(array $data): self
205211
{
206212
return self::converter()->from($data); // @phpstan-ignore-line
207213
}

src/Core/Concerns/SdkPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function hasNextPage(): bool
5050
* Before calling this method, you must check if there is a next page
5151
* using {@link hasNextPage()}.
5252
*
53-
* @return static of AbstractPage<Item>
53+
* @return static of static<Item>
5454
*
5555
* @throws APIStatusError
5656
*/

src/Core/Contracts/BasePage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public function __construct(
3333
);
3434

3535
/**
36+
* @param array<string, mixed> $data
37+
*
3638
* @return static<Item>
3739
*/
38-
public static function fromArray(mixed $data): self;
40+
public static function fromArray(array $data): self;
3941

4042
/** @return array<string, mixed> */
4143
public function toArray(): array;

src/Crawl/CrawlGetResultsResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
use Scrapegraphai\Crawl\CrawlGetResultsResponse\Result;
1111
use Scrapegraphai\Crawl\CrawlGetResultsResponse\Status;
1212

13+
/**
14+
* @phpstan-type crawl_get_results_response = array{
15+
* result?: mixed|string|null,
16+
* status?: Status::*|null,
17+
* taskID?: string|null,
18+
* traceback?: string|null,
19+
* }
20+
*/
1321
final class CrawlGetResultsResponse implements BaseModel
1422
{
23+
/** @use SdkModel<crawl_get_results_response> */
1524
use SdkModel;
1625

1726
/**

src/Crawl/CrawlStartParams.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,22 @@
1414
* Initiate comprehensive website crawling with sitemap support.
1515
* Supports both AI extraction mode and markdown conversion mode.
1616
* Returns a task ID for async processing.
17+
*
18+
* @phpstan-type crawl_start_params = array{
19+
* url: string,
20+
* depth?: int,
21+
* extractionMode?: bool,
22+
* maxPages?: int,
23+
* prompt?: string|null,
24+
* renderHeavyJs?: bool,
25+
* rules?: Rules,
26+
* schema?: mixed,
27+
* sitemap?: bool,
28+
* }
1729
*/
1830
final class CrawlStartParams implements BaseModel
1931
{
32+
/** @use SdkModel<crawl_start_params> */
2033
use SdkModel;
2134
use SdkParams;
2235

src/Crawl/CrawlStartParams/Rules.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
use Scrapegraphai\Core\Concerns\SdkModel;
99
use Scrapegraphai\Core\Contracts\BaseModel;
1010

11+
/**
12+
* @phpstan-type rules_alias = array{
13+
* exclude?: list<string>|null, sameDomain?: bool|null
14+
* }
15+
*/
1116
final class Rules implements BaseModel
1217
{
18+
/** @use SdkModel<rules_alias> */
1319
use SdkModel;
1420

1521
/**

src/Crawl/CrawlStartResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
use Scrapegraphai\Core\Concerns\SdkModel;
99
use Scrapegraphai\Core\Contracts\BaseModel;
1010

11+
/**
12+
* @phpstan-type crawl_start_response = array{taskID?: string|null}
13+
*/
1114
final class CrawlStartResponse implements BaseModel
1215
{
16+
/** @use SdkModel<crawl_start_response> */
1317
use SdkModel;
1418

1519
/**

src/Credits/CreditGetResponse.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
use Scrapegraphai\Core\Concerns\SdkModel;
99
use Scrapegraphai\Core\Contracts\BaseModel;
1010

11+
/**
12+
* @phpstan-type credit_get_response = array{
13+
* remainingCredits?: int|null, totalCreditsUsed?: int|null
14+
* }
15+
*/
1116
final class CreditGetResponse implements BaseModel
1217
{
18+
/** @use SdkModel<credit_get_response> */
1319
use SdkModel;
1420

1521
/**

src/Feedback/FeedbackSubmitParams.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111

1212
/**
1313
* Submit feedback for a specific request.
14+
*
15+
* @phpstan-type feedback_submit_params = array{
16+
* rating: int, requestID: string, feedbackText?: string|null
17+
* }
1418
*/
1519
final class FeedbackSubmitParams implements BaseModel
1620
{
21+
/** @use SdkModel<feedback_submit_params> */
1722
use SdkModel;
1823
use SdkParams;
1924

src/Feedback/FeedbackSubmitResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
use Scrapegraphai\Core\Concerns\SdkModel;
99
use Scrapegraphai\Core\Contracts\BaseModel;
1010

11+
/**
12+
* @phpstan-type feedback_submit_response = array{
13+
* feedbackID?: string|null,
14+
* feedbackTimestamp?: \DateTimeInterface|null,
15+
* message?: string|null,
16+
* requestID?: string|null,
17+
* }
18+
*/
1119
final class FeedbackSubmitResponse implements BaseModel
1220
{
21+
/** @use SdkModel<feedback_submit_response> */
1322
use SdkModel;
1423

1524
#[Api('feedback_id', optional: true)]

0 commit comments

Comments
 (0)