From 32acac4549e8ee6d16bbb195a45fb885544347dd Mon Sep 17 00:00:00 2001 From: Quentin Ra Date: Sat, 2 Oct 2021 23:33:29 +0200 Subject: [PATCH 1/4] adding code_with_match (#1024) --- doc/search.md | 8 +++++++- lib/Github/Api/Search.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/search.md b/doc/search.md index 0a674808001..e0f174badfa 100644 --- a/doc/search.md +++ b/doc/search.md @@ -13,13 +13,19 @@ $repos = $client->api('search')->repositories('github language:php'); Returns a list of repositories found by such criteria. ### Search code - + ```php $files = $client->api('search')->code('@todo language:php'); ``` Returns a list of files found by such criteria (containing "@todo" and language==php). +```php +$files = $client->api('search')->code_with_match('@todo language:php'); +``` + +Same as code, with additional data to highlight the matching fragments (see [Text match metadata](https://docs.github.com/en/rest/reference/search#text-match-metadata)). + ### Search issues ```php diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 24bd59bbf83..24816cc218f 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -61,6 +61,24 @@ public function code($q, $sort = 'updated', $order = 'desc') return $this->get('/search/code', ['q' => $q, 'sort' => $sort, 'order' => $order]); } + /** + * Search code by filter (q), but will return additional data to highlight + * the matched results. + * + * @link https://docs.github.com/en/rest/reference/search#text-match-metadata + * + * @param string $q the filter + * @param string $sort the sort field + * @param string $order asc/desc + * + * @return array list of code found + */ + public function code_with_match($q, $sort = 'updated', $order = 'desc') + { + $this->acceptHeaderValue = 'application/vnd.github.v3.text-match+json'; + return $this->code($q, $sort, $order); + } + /** * Search users by filter (q). * From 179532a2f785787ddc0a52d0de9e513e41ff79f5 Mon Sep 17 00:00:00 2001 From: Quentin Ra Date: Sat, 2 Oct 2021 23:38:09 +0200 Subject: [PATCH 2/4] adding a new line --- lib/Github/Api/Search.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 24816cc218f..67d05b981e1 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -76,6 +76,7 @@ public function code($q, $sort = 'updated', $order = 'desc') public function code_with_match($q, $sort = 'updated', $order = 'desc') { $this->acceptHeaderValue = 'application/vnd.github.v3.text-match+json'; + return $this->code($q, $sort, $order); } From c3be9515e3d81b0c3fc2f5f05a242cb9ed8ea7a3 Mon Sep 17 00:00:00 2001 From: Quentin Ra Date: Sun, 3 Oct 2021 12:29:39 +0200 Subject: [PATCH 3/4] changes requested --- doc/search.md | 2 +- lib/Github/Api/Search.php | 8 +++--- test/Github/Tests/Api/SearchTest.php | 43 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/doc/search.md b/doc/search.md index e0f174badfa..24dbbef2302 100644 --- a/doc/search.md +++ b/doc/search.md @@ -21,7 +21,7 @@ $files = $client->api('search')->code('@todo language:php'); Returns a list of files found by such criteria (containing "@todo" and language==php). ```php -$files = $client->api('search')->code_with_match('@todo language:php'); +$files = $client->api('search')->codeWithMatch('@todo language:php'); ``` Same as code, with additional data to highlight the matching fragments (see [Text match metadata](https://docs.github.com/en/rest/reference/search#text-match-metadata)). diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 67d05b981e1..57ec50e5964 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -67,13 +67,13 @@ public function code($q, $sort = 'updated', $order = 'desc') * * @link https://docs.github.com/en/rest/reference/search#text-match-metadata * - * @param string $q the filter - * @param string $sort the sort field - * @param string $order asc/desc + * @param $q the filter + * @param $sort the sort field + * @param $order asc/desc * * @return array list of code found */ - public function code_with_match($q, $sort = 'updated', $order = 'desc') + public function codeWithMatch(string $q, string $sort = 'updated', string $order = 'desc'): array { $this->acceptHeaderValue = 'application/vnd.github.v3.text-match+json'; diff --git a/test/Github/Tests/Api/SearchTest.php b/test/Github/Tests/Api/SearchTest.php index a44c7b7499e..feecb7b5a74 100644 --- a/test/Github/Tests/Api/SearchTest.php +++ b/test/Github/Tests/Api/SearchTest.php @@ -133,6 +133,49 @@ public function shouldSearchCodeRegardingSortAndOrder() ); } + /** + * @test + */ + public function shouldSearchCodeWithMatchByQuery() + { + $expectedArray = [['total_count' => '0']]; + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/code', + ['q' => 'query text', 'sort' => 'updated', 'order' => 'desc'] + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->codeWithMatch('query text')); + } + + /** + * @test + */ + public function shouldSearchCodeWithMatchRegardingSortAndOrder() + { + $expectedArray = [['total_count' => '0']]; + + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with( + '/search/code', + ['q' => 'query text', 'sort' => 'created', 'order' => 'asc'] + ) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals( + $expectedArray, + $api->codeWithMatch('query text', 'created', 'asc') + ); + } + /** * @test */ From f89811e2091f8a5716fb4a2f2c2af562a207e73c Mon Sep 17 00:00:00 2001 From: Quentin Ra Date: Sun, 3 Oct 2021 18:27:25 +0200 Subject: [PATCH 4/4] removing the whole phpdoc comment --- lib/Github/Api/Search.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/Github/Api/Search.php b/lib/Github/Api/Search.php index 57ec50e5964..96a51ea6a46 100644 --- a/lib/Github/Api/Search.php +++ b/lib/Github/Api/Search.php @@ -67,10 +67,6 @@ public function code($q, $sort = 'updated', $order = 'desc') * * @link https://docs.github.com/en/rest/reference/search#text-match-metadata * - * @param $q the filter - * @param $sort the sort field - * @param $order asc/desc - * * @return array list of code found */ public function codeWithMatch(string $q, string $sort = 'updated', string $order = 'desc'): array