Skip to content

Commit

Permalink
fix: add readme snippets to checks for static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Jan 19, 2024
1 parent 10cb3b3 commit 769967a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ vendor/
tests/coverage
.idea
.phpunit.result.cache
.phpunit.cache
.phpunit.cachetests/snippets
tests/snippets
51 changes: 31 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ Usage Example
#### Producer

```php
<?php
require __DIR__ . '/vendor/autoload.php';

use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;

Expand All @@ -34,19 +31,16 @@ $pheanstalk->put("job payload goes here\n");

$pheanstalk->useTube($tube);
$pheanstalk->put(
data: json_encode(['test' => 'data']),
data: json_encode(['test' => 'data'], JSON_THROW_ON_ERROR),
priority: Pheanstalk::DEFAULT_PRIORITY,
delay: 30,
timeToRelease: 60
);

```


#### Consumer / Worker
```php
<?php
require __DIR__ . '/vendor/autoload.php';
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;

Expand All @@ -62,6 +56,8 @@ $job = $pheanstalk->reserve();
try {
$jobPayload = $job->getData();
// do work.

echo "Starting job with payload: {$jobPayload}\n";

sleep(2);
// If it's going to take a long time, periodically
Expand Down Expand Up @@ -125,25 +121,40 @@ or use `reserveWithTimeout()` with a timeout that is less than the read / write

Example code for a job runner could look like this (this is real production code):
```php
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;

$pheanstalk = Pheanstalk::create('127.0.0.1');

class Task {
public static function fromData(string $data): self {
return new self();
}
}
class CommandBus {
public function handle(Task $task): void {
}
}

$commandBus = new CommandBus();

/**
* @phpstan-ignore-next-line
*/
while(true) {
$job = $beanstalk->reserveWithTimeout(50);
$this->stdout('.', Console::FG_CYAN);
$job = $pheanstalk->reserveWithTimeout(50);
echo ".";
if (isset($job)) {
$this->ensureDatabase($db);
try {
/** @var HookTask $task */
$task = $taskFactory->createFromJson($job->getData());

$task = Task::fromData($job->getData());

$commandBus->handle($task);
$this->stdout("Deleting job: {$job->getId()}\n", Console::FG_GREEN);
$beanstalk->delete($job);
echo "Deleting job: {$job->getId()}\n";
$pheanstalk->delete($job);
} catch (\Throwable $t) {
\Yii::error($t);
$this->stderr("\n{$t->getMessage()}\n", Console::FG_RED);
$this->stderr("{$t->getTraceAsString()}\n", Console::FG_RED);

$this->stdout("Burying job: {$job->getId()}\n", Console::FG_YELLOW);
$beanstalk->bury($job);
echo "Burying job: {$job->getId()}\n";
$pheanstalk->bury($job);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
$config->paths([
__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php'
]);
$config->skip([
__DIR__ . '/tests/snippets'
]);
// A. full sets
$config->import(SetList::PSR_12);
$config->import(SetList::STRICT);
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ parameters:
treatPhpDocTypesAsCertain: false
reportUnmatchedIgnoredErrors: true
bootstrapFiles:
- tests/bootstrap.php
- tests/sa-bootstrap.php
includes:
- phpstan-baseline.neon
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
autoloader="tests/bootstrap.php"
autoloader="tests/sa-bootstrap.php"
>
<projectFiles>
<directory name="src" />
Expand Down
56 changes: 56 additions & 0 deletions tests/sa-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

require __DIR__ . '/bootstrap.php';

// Extract snippets from readme.
$readme = file(__DIR__ . '/../README.md', FILE_SKIP_EMPTY_LINES + FILE_IGNORE_NEW_LINES);
if ($readme === false) {
throw new \RuntimeException("Failed to open readme file");
}

passthru('rm -r ' . __DIR__ . '/snippets');
mkdir(__DIR__ . '/snippets');
/**
* @param string[] $lines
* @return string[]
*/
function readSnippet(&$lines): array
{
$snippet = [];
while ($lines[0] !== '```') {
$snippet[] = array_shift($lines);
}
array_shift($lines);
return $snippet;
}

/**
* @param string[] $snippet
* @param string $name
* @return void
*/
function storeSnippet(array $snippet, string $name)
{
// We replace occurrences of __DIR__

$fileName = __DIR__ . '/snippets/' . strtr($name, [' ' => '-', '/' => '_or_']) . '.php';

file_put_contents($fileName, strtr(implode("\n", ["<?php", ...$snippet]), [
'__DIR__' => '__DIR__ . "/.."'
]));
}

$title = 'Root';
while ($readme !== []) {
$line = array_shift($readme);
if (preg_match('~^\#+\s+(.*)$~', $line, $matches) === 1) {
$title = $matches[1];
}

if ($line === '```php') {
$snippet = readSnippet($readme);
storeSnippet($snippet, $title);
}
}

0 comments on commit 769967a

Please sign in to comment.