<commit_before><?php declare(strict_types = 1);

namespace App;

use App\BotCommentParser\BotCommentParser;
use App\BotCommentParser\BotCommentParserException;
use App\Playground\PlaygroundClient;
use App\Playground\PlaygroundExample;
use AppendIterator;
use DateTimeImmutable;
use Github\Client;
use Github\HttpClient\Builder;
use GuzzleHttp\Promise\Utils;
use Iterator;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

require_once __DIR__ . '/vendor/autoload.php';

$token = $_SERVER['GITHUB_PAT'];
$phpstanSrcCommitBefore = $_SERVER['PHPSTAN_SRC_COMMIT_BEFORE'];
$phpstanSrcCommitAfter = $_SERVER['PHPSTAN_SRC_COMMIT_AFTER'];

$rateLimitPlugin = new RateLimitPlugin();

$gitHubRequestCounter = new RequestCounterPlugin();

$httpBuilder = new Builder();
$httpBuilder->addPlugin($rateLimitPlugin);
$httpBuilder->addPlugin($gitHubRequestCounter);

$client = new Client($httpBuilder);
$client->authenticate($token, Client::AUTH_ACCESS_TOKEN);

$rateLimitPlugin->setClient($client);

$playgroundClient = new PlaygroundClient(new \GuzzleHttp\Client());

$markdownEnvironment = Environment::createCommonMarkEnvironment();
$markdownEnvironment->addExtension(new GithubFlavoredMarkdownExtension());
$botCommentParser = new BotCommentParser(new DocParser($markdownEnvironment));

/**
 * @param string $label
 * @return Iterator<int, Issue>
 */
function getIssues(string $label): Iterator
{
	/** @var Client */
	global $client;

	$page = 1;

	/** @var \Github\Api\Issue $api */
	$api = $client->api('issue');
	while (true) {
		$newIssues = $api->all('phpstan', 'phpstan', [
			'state' => 'open',
			'labels' => $label,
			'page' => $page,
			'per_page' => 100,
			'sort' => 'created',
			'direction' => 'desc',
		]);
		if (count($newIssues) === 0) {
			break;
		}

		yield from array_map(function (array $issue): Issue {
			return new Issue(
				$issue['number'],
				$issue['user']['login'],
				$issue['body'],
				DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $issue['updated_at']), // @phpstan-ignore-line
				getComments($issue['number']),
				searchBody($issue['body'], $issue['user']['login'])
			);
		}, $newIssues);
		$page++;
	}
}

/**
 * @param int $issueNumber
 * @return Comment[]
 */
function getComments(int $issueNumber): iterable
{
	/** @var Client */
	global $client;
	$page = 1;

	/** @var BotCommentParser */
	global $botCommentParser;

	/** @var \Github\Api\Issue $api */
	$api = $client->api('issue');<commit_msg>Workaround for GitHub API HTTP 500<commit_after><?php declare(strict_types = 1);

namespace App;

use App\BotCommentParser\BotCommentParser;
use App\BotCommentParser\BotCommentParserException;
use App\Playground\PlaygroundClient;
use App\Playground\PlaygroundExample;
use AppendIterator;
use DateTimeImmutable;
use Github\Client;
use Github\HttpClient\Builder;
use GuzzleHttp\Promise\Utils;
use Iterator;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

require_once __DIR__ . '/vendor/autoload.php';

$token = $_SERVER['GITHUB_PAT'];
$phpstanSrcCommitBefore = $_SERVER['PHPSTAN_SRC_COMMIT_BEFORE'];
$phpstanSrcCommitAfter = $_SERVER['PHPSTAN_SRC_COMMIT_AFTER'];

$rateLimitPlugin = new RateLimitPlugin();

$gitHubRequestCounter = new RequestCounterPlugin();

$httpBuilder = new Builder();
$httpBuilder->addPlugin($rateLimitPlugin);
$httpBuilder->addPlugin($gitHubRequestCounter);

$client = new Client($httpBuilder);
$client->authenticate($token, Client::AUTH_ACCESS_TOKEN);

$rateLimitPlugin->setClient($client);

$playgroundClient = new PlaygroundClient(new \GuzzleHttp\Client());

$markdownEnvironment = Environment::createCommonMarkEnvironment();
$markdownEnvironment->addExtension(new GithubFlavoredMarkdownExtension());
$botCommentParser = new BotCommentParser(new DocParser($markdownEnvironment));

/**
 * @param string $label
 * @return Iterator<int, Issue>
 */
function getIssues(string $label): Iterator
{
	/** @var Client */
	global $client;

	$page = 1;

	/** @var \Github\Api\Issue $api */
	$api = $client->api('issue');
	while (true) {
		$newIssues = $api->all('phpstan', 'phpstan', [
			'state' => 'open',
			'labels' => $label,
			'page' => $page,
			'per_page' => 100,
			'sort' => 'created',
			'direction' => 'desc',
		]);
		if (count($newIssues) === 0) {
			break;
		}

		yield from array_map(function (array $issue): Issue {
			return new Issue(
				$issue['number'],
				$issue['user']['login'],
				$issue['body'],
				DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $issue['updated_at']), // @phpstan-ignore-line
				getComments($issue['number']),
				searchBody($issue['body'], $issue['user']['login'])
			);
		}, $newIssues);
		$page++;
	}
}

/**
 * @param int $issueNumber
 * @return Comment[]
 */
function getComments(int $issueNumber): iterable
{
	if ($issueNumber === 7454) {
		return [];
	}

	/** @var Client */
	global $client;
	$page = 1;

	/** @var BotCommentParser */
	global $botCommentParser;

	/** @var \Github\Api\Issue $api */
	$api = $client->api('issue');
{'fraction_non_alphanumeric': 0.1612721114038389, 'fraction_numerical': 0.003951825366955213, 'mean_word_length': 5.84041184041184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3694946', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1710, 'n_words': 326}