Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/bedrock-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ on:
concurrency:
group: "${{ github.ref }}"
cancel-in-progress: true
env:
TRAVIS_COMMIT: ${{ github.sha }}
TRAVIS_BRANCH: ${{ github.ref }}
jobs:
PHP_Phan:
name: Phan
Expand Down Expand Up @@ -42,7 +39,7 @@ jobs:
PHP_Style:
name: PHP Style
runs-on: ubuntu-24.04
timeout-minutes: 15
timeout-minutes: 60
steps:
- name: checkout
# v4.1.0
Expand All @@ -64,5 +61,12 @@ jobs:
- name: Setup Composer Cache
uses: ./.github/actions/composite/setup-composer-cache

- name: Setup tmate session
# v3
uses: mxschmitt/action-tmate@e5c7151931ca95bad1c6f4190c730ecf8c7dde48
timeout-minutes: 60
with:
limit-access-to-actor: true

- name: Test for Style
run: "php ./ci/style.php"
6 changes: 3 additions & 3 deletions ci/phan.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);
require realpath(dirname(__FILE__)).'/../vendor/autoload.php';
use Expensify\Bedrock\CI\PhanAnalyzer;

$analyzer = new PhanAnalyzer($_SERVER['TRAVIS_BRANCH']);
$analyzer = new PhanAnalyzer($_SERVER['GITHUB_REF']);
if ($analyzer->analyze()) {
exit(0);
} else {
exit(1);
}
exit(1);
4 changes: 2 additions & 2 deletions ci/src/CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class CommandLine
*/
protected function checkoutBranch($branch)
{
Travis::fold("start", "git.checkout.".get_called_class());
Travis::fold('start', 'git.checkout.'.get_called_class());
$this->call("git checkout $branch 2>&1");
$this->call('git fetch origin main:main 2>&1');
Travis::fold("end", "git.checkout.".get_called_class());
Travis::fold('end', 'git.checkout.'.get_called_class());
}

/**
Expand Down
18 changes: 11 additions & 7 deletions ci/src/PHPStyler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
class PHPStyler extends CommandLine
{
/**
* @var string Branch we are checking (usually coming from['TRAVIS_BRANCH'])
* @var string Branch we are checking (usually coming from['GITHUB_REF'])
*/
private $branch;

/**
* @var string Commit we are checking (usually coming from['TRAVIS_COMMIT'])
* @var string Commit we are checking (usually coming from['GITHUB_SHA'])
*/
private $commit;

Expand All @@ -39,7 +39,7 @@ public function check()
$PHPLintCommand = "find . -name '*.php' -not \\( -path './externalLib/*' -or -path './vendor/*' -or -path './build/*' \\) -print0 | xargs -0 -L 1 -n 1 -P 8 php -l 1>/dev/null";

if ($this->branch === 'main') {
Travis::foldCall("lintmaster.php", $PHPLintCommand);
Travis::foldCall('lintmaster.php', $PHPLintCommand);

echo 'Skipping style check for merge commits';

Expand All @@ -48,9 +48,9 @@ public function check()

$this->checkoutBranch($this->branch);

Travis::foldCall("lint.php", $PHPLintCommand);
Travis::foldCall('lint.php', $PHPLintCommand);

Travis::fold("start", "style.php");
Travis::fold('start', 'style.php');
Travis::timeStart();
echo 'Enforce PHP style'.PHP_EOL;
$output = $this->getModifiedFiles($this->branch);
Expand Down Expand Up @@ -83,13 +83,17 @@ public function check()
}

Travis::timeFinish();
Travis::fold("end", "style.php");
Travis::fold('end', 'style.php');

if (!$lintOK) {
return false;
}

Travis::foldCall("git.checkout2", "git checkout {$this->commit} 2>&1");
echo 'post-checkout2';

Travis::foldCall('git.checkout2', 'git checkout {$this->commit} 2>&1');

echo 'post-checkout2';

return true;
}
Expand Down
12 changes: 6 additions & 6 deletions ci/src/PhanAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class PhanAnalyzer extends CommandLine
{
/**
* @var string Branch we are checking (usually coming from['TRAVIS_BRANCH'])
* @var string Branch we are checking (usually coming from['GITHUB_REF'])
*/
private $branch;

Expand All @@ -35,12 +35,12 @@ public function analyze()

$this->checkoutBranch($this->branch);

Travis::fold("start", "phan.analyze");
Travis::fold('start', 'phan.analyze');
Travis::timeStart();
echo 'Analyze PHP using Phan'.PHP_EOL;
$changedFiles = $this->eexec("git diff main...{$this->branch} --name-status | egrep \"^[A|M].*\\.php$\" | cut -f 2");
echo "Analyzing files:".PHP_EOL;
$lintErrors = $this->eexec("./vendor/bin/phan -p -z --processes 5", false);
$changedFiles = $this->eexec('git diff main...{$this->branch} --name-status | egrep \"^[A|M].*\\.php$\" | cut -f 2');
echo 'Analyzing files:'.PHP_EOL;
$lintErrors = $this->eexec('./vendor/bin/phan -p -z --processes 5', false);
$lintOK = true;
foreach ($lintErrors as $lintError) {
foreach ($changedFiles as $file) {
Expand All @@ -52,7 +52,7 @@ public function analyze()
}
}
Travis::timeFinish();
Travis::fold("end", "phan.analyze");
Travis::fold('end', 'phan.analyze');

return $lintOK;
}
Expand Down
12 changes: 6 additions & 6 deletions ci/src/Travis.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class Travis
{
const ANSI_CLEAR = "\033[0K";
public const ANSI_CLEAR = "\033[0K";

protected static $timerID;
protected static $timerStart;
Expand All @@ -19,7 +19,7 @@ public static function fold($action, $foldName)

public static function foldCall($foldName, $cmd, $includeTimer = true)
{
self::fold("start", $foldName);
self::fold('start', $foldName);

if ($includeTimer) {
self::timeStart();
Expand All @@ -31,7 +31,7 @@ public static function foldCall($foldName, $cmd, $includeTimer = true)
self::timeFinish();
}

self::fold("end", $foldName);
self::fold('end', $foldName);

if ($retVal !== 0) {
exit($retVal);
Expand All @@ -40,17 +40,17 @@ public static function foldCall($foldName, $cmd, $includeTimer = true)

public static function timeStart()
{
self::$timerID = sprintf("%08x", rand() * rand());
self::$timerID = sprintf('%08x', rand() * rand());
self::$timerStart = self::getNanosecondsTime();
echo "travis_time:start:".self::$timerID."\r".self::ANSI_CLEAR;
echo 'travis_time:start:'.self::$timerID."\r".self::ANSI_CLEAR;
}

public static function timeFinish()
{
$start = self::$timerStart;
$end = self::getNanosecondsTime();
$duration = bcsub($end, $start);
echo "travis_time:end:".self::$timerID.":start=$start,finish=$end,duration=$duration\r".self::ANSI_CLEAR;
echo 'travis_time:end:'.self::$timerID.":start=$start,finish=$end,duration=$duration\r".self::ANSI_CLEAR;
}

public static function getNanosecondsTime()
Expand Down
4 changes: 3 additions & 1 deletion ci/style.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require realpath(dirname(__FILE__)).'/../vendor/autoload.php';
use Expensify\Bedrock\CI\PHPStyler;

$styler = new PHPStyler($_SERVER['TRAVIS_BRANCH'], $_SERVER['TRAVIS_COMMIT']);
$styler = new PHPStyler($_SERVER['GITHUB_REF'], $_SERVER['GITHUB_SHA']);
$valid = $styler->check();
echo 'this should still work';
echo $valid;
exit((int) !$valid);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "expensify/bedrock-php",
"description": "Bedrock PHP Library",
"type": "library",
"version": "2.2.2",
"version": "2.2.4",
"authors": [
{
"name": "Expensify",
Expand Down
1 change: 1 addition & 0 deletions sample/SampleWorker.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Sample BedrockWorkerManager worker class.
*/
Expand Down
19 changes: 6 additions & 13 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class Cache extends Plugin
public function read($name, $version = null)
{
$fullName = ($version ? "$name/$version" : "$name/*");
$this->client->getLogger()->info("BedrockCache read", [
$this->client->getLogger()->info('BedrockCache read', [
'key' => $name,
'version' => $version,
]);
$response = $this->call("ReadCache", ["name" => $fullName]);
$response = $this->call('ReadCache', ['name' => $fullName]);
if ($response['code'] === 404) {
throw new NotFound('The cache entry could not be found', 666);
}
Expand All @@ -41,10 +41,7 @@ public function read($name, $version = null)
* Reads from the cache, but if it does not find the entry, it returns the passed default.
*
* @param string $name
* @param mixed $default
* @param string $version
*
* @return mixed
*/
public function readWithDefault($name, $default, $version = null)
{
Expand All @@ -58,10 +55,6 @@ public function readWithDefault($name, $default, $version = null)
/**
* Gets data from a cache, if it is not present, it computes it by calling $computeFunction and saves the result in the cache.
*
* @param string $name
* @param null|string $version
* @param callable $computeFunction
*
* @return array
*/
public function get(string $name, ?string $version, callable $computeFunction)
Expand Down Expand Up @@ -96,14 +89,14 @@ public function write($name, $value, $version = null, array $headers = [])
// If we have a version, invalidate previous versions
if ($version) {
// Invalidate all other versions of this name before setting
$headers["invalidateName"] = "$name/*";
$headers["name"] = "$name/$version";
$headers['invalidateName'] = "$name/*";
$headers['name'] = "$name/$version";
} else {
// Just set this name
$headers["name"] = "$name/";
$headers['name'] = "$name/";
}

$this->call("WriteCache", $headers, json_encode($value));
$this->call('WriteCache', $headers, json_encode($value));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ private function sendRawRequest(string $host, int $port, string $rawRequest)
* @param ?string $preferredHost If passed, it will prefer this host over any of the configured ones. This does not
* ensure it will use that host, but it will try to use it if its not blacklisted.
*
* @suppress PhanUndeclaredConstant - suppresses TRAVIS_RUNNING
* @suppress PhanUndeclaredConstant - suppresses ARE_GITHUB_ACTIONS_RUNNING
*/
private function getPossibleHosts(?string $preferredHost, bool $resetHosts = false)
{
// We get the host configs from the APC cache. Then, we check the configuration there with the passed
// configuration and if it's outdated (ie: it has different hosts from the one in the config), we reset it. This
// is so that we don't keep the old cache after changing the hosts or failover configuration.
if (!defined('TRAVIS_RUNNING') || !TRAVIS_RUNNING) {
if (!defined('ARE_GITHUB_ACTIONS_RUNNING') || !ARE_GITHUB_ACTIONS_RUNNING) {
$apcuKey = self::APCU_CACHE_PREFIX.$this->clusterName;
if ($resetHosts) {
$this->logger->info('Bedrock\Client - Resetting host configs');
Expand Down Expand Up @@ -838,12 +838,12 @@ private static function toUTF8($str)
* configuration.
* We also close and clear the socket from the cache, so we don't reuse it.
*
* @suppress PhanUndeclaredConstant - suppresses TRAVIS_RUNNING
* @suppress PhanUndeclaredConstant - suppresses ARE_GITHUB_ACTIONS_RUNNING
*/
private function markHostAsFailed(string $host)
{
$blacklistedUntil = time() + rand(1, $this->maxBlackListTimeout);
if (!defined('TRAVIS_RUNNING') || !TRAVIS_RUNNING) {
if (!defined('ARE_GITHUB_ACTIONS_RUNNING') || !ARE_GITHUB_ACTIONS_RUNNING) {
$apcuKey = self::APCU_CACHE_PREFIX.$this->clusterName;
$hostConfigs = apcu_fetch($apcuKey);
$hostConfigs[$host]['blacklistedUntil'] = $blacklistedUntil;
Expand Down
10 changes: 5 additions & 5 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class DB extends Plugin
*
* @var int
*/
const CODE_OK = 200;
public const CODE_OK = 200;

/**
* Failed query response code.
*
* @var int
*/
const CODE_QUERY_FAILED = 502;
public const CODE_QUERY_FAILED = 502;

/**
* Executes a single SQL query.
Expand Down Expand Up @@ -52,7 +52,7 @@ public function query($sql): Response
*/
public function run(string $sql, bool $idempotent, int $timeout = 60000): Response
{
$sql = substr($sql, -1) === ";" ? $sql : $sql.";";
$sql = substr($sql, -1) === ';' ? $sql : $sql.';';
$matches = [];
preg_match('/\s*(select|insert|delete|update).*/i', $sql, $matches);
$operation = isset($matches[1]) && in_array(strtolower($matches[1]), ['insert', 'update', 'delete', 'select']) ? strtolower($matches[1]) : 'unknown';
Expand All @@ -61,15 +61,15 @@ public function run(string $sql, bool $idempotent, int $timeout = 60000): Respon
'Query',
[
'query' => $sql,
'format' => "json",
'format' => 'json',
'idempotent' => $idempotent,
'timeout' => $timeout,
]
));
});

if ($response->getCode() === self::CODE_QUERY_FAILED) {
throw new BedrockError($response->getCodeLine()." - ".$response->getError(), $response->getCode());
throw new BedrockError($response->getCodeLine().' - '.$response->getError(), $response->getCode());
}

if ($response->getCode() !== self::CODE_OK) {
Expand Down
8 changes: 1 addition & 7 deletions src/DB/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class Response implements JsonSerializable, Countable
{
protected $container = null;
protected $container;

public function __construct(array $leValue)
{
Expand Down Expand Up @@ -47,9 +47,6 @@ public function jsonSerialize(): array
* Shortcut method to access the value inside the container.
*
* @param string|array $index
* @param mixed $default
*
* @return mixed
*/
protected function getFromContainer($index, $default = null)
{
Expand Down Expand Up @@ -80,9 +77,6 @@ protected function getFromContainer($index, $default = null)
* Shortcut function to set data inside the container.
*
* @param string|int|array $index
* @param mixed $value
*
* @return mixed
*/
protected function setContainerValue($index, $value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/BedrockError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class BedrockError extends Exception
{
public function __construct($message, $code = 666, Exception $previous = null)
public function __construct($message, $code = 666, ?Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
Loading
Loading