Skip to content

Commit

Permalink
Fix PHP 8.4 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Dec 8, 2024
1 parent 3aac213 commit 94a6590
Show file tree
Hide file tree
Showing 60 changed files with 94 additions and 244 deletions.
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$config = new class extends Amp\CodeStyle\Config {
public function getRules(): array
{
$rules = parent::getRules();

$rules['declare_strict_types'] = false;

return $rules;
}
};

$config->getFinder()
->in(__DIR__ . '/examples')
->in(__DIR__ . '/lib')
->in(__DIR__ . '/test');

$config->setCacheFile(__DIR__ . '/.php_cs.cache');

return $config;
13 changes: 0 additions & 13 deletions .php_cs.dist

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"amphp/sync": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^8 || ^7",
"phpunit/phpunit": "^9 || ^8 || ^7",
"amphp/phpunit-util": "^1.1",
"amphp/php-cs-fixer-config": "dev-master"
"amphp/php-cs-fixer-config": "^2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions examples/blocking-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
use Amp\Parallel\Sync\Channel;

return function (Channel $channel): \Generator {
\printf("Received the following from parent: %s\n", yield $channel->receive());
printf("Received the following from parent: %s\n", yield $channel->receive());

print "Sleeping for 3 seconds...\n";
\sleep(3); // Blocking call in process.
sleep(3); // Blocking call in process.

yield $channel->send("Data sent from child.");

print "Sleeping for 2 seconds...\n";
\sleep(2); // Blocking call in process.
sleep(2); // Blocking call in process.

return 42;
};
9 changes: 5 additions & 4 deletions examples/parallel-extension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__).'/vendor/autoload.php';

require dirname(__DIR__).'/vendor/autoload.php';

use Amp\Delayed;
use Amp\Loop;
Expand All @@ -17,15 +18,15 @@
// Create a new child thread that does some blocking stuff.
$context = yield Parallel::run(__DIR__ . "/blocking-process.php");

\assert($context instanceof Parallel);
assert($context instanceof Parallel);

print "Waiting 2 seconds to send start data...\n";
yield new Delayed(2000);

yield $context->send("Start data"); // Data sent to child process, received on line 9 of blocking-process.php

\printf("Received the following from child: %s\n", yield $context->receive()); // Sent on line 14 of blocking-process.php
\printf("Process ended with value %d!\n", yield $context->join());
printf("Received the following from child: %s\n", yield $context->receive()); // Sent on line 14 of blocking-process.php
printf("Process ended with value %d!\n", yield $context->join());
} finally {
Loop::cancel($timer);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/parcel-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

$id = $argv[1];

\printf("Child process using parcel ID %s\n", $id);
printf("Child process using parcel ID %s\n", $id);

$parcel = SharedMemoryParcel::use($id);

$value = yield $parcel->synchronized(function (int $value) {
return $value + 1;
});

\printf("Value after modifying in child thread: %s\n", $value);
printf("Value after modifying in child thread: %s\n", $value);

yield new Delayed(500); // Parent process should access parcel during this time.

// Unwrapping the parcel now should give value from parent process.
\printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap());
printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap());

yield $parcel->synchronized(function (int $value) {
return $value + 1;
Expand Down
9 changes: 5 additions & 4 deletions examples/process.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__).'/vendor/autoload.php';

require dirname(__DIR__).'/vendor/autoload.php';

use Amp\ByteStream;
use Amp\Delayed;
Expand All @@ -18,7 +19,7 @@
// Create a new child process that does some blocking stuff.
$context = yield Process::run(__DIR__ . "/blocking-process.php");

\assert($context instanceof Process);
assert($context instanceof Process);

// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
Expand All @@ -28,8 +29,8 @@

yield $context->send("Start data"); // Data sent to child process, received on line 9 of blocking-process.php

\printf("Received the following from child: %s\n", yield $context->receive()); // Sent on line 14 of blocking-process.php
\printf("Process ended with value %d!\n", yield $context->join());
printf("Received the following from child: %s\n", yield $context->receive()); // Sent on line 14 of blocking-process.php
printf("Process ended with value %d!\n", yield $context->join());
} finally {
Loop::cancel($timer);
}
Expand Down
9 changes: 5 additions & 4 deletions examples/shared-memory-parcel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__).'/vendor/autoload.php';

require dirname(__DIR__).'/vendor/autoload.php';

use Amp\ByteStream;
use Amp\Delayed;
Expand All @@ -10,14 +11,14 @@

Loop::run(function () {
// Create a parcel that then can be accessed in any number of child processes.
$parcel = SharedMemoryParcel::create($id = \bin2hex(\random_bytes(10)), 1);
$parcel = SharedMemoryParcel::create($id = bin2hex(random_bytes(10)), 1);

$context = yield Process::run([
__DIR__ . "/parcel-process.php",
$id, // Send parcel ID to child process as command argument.
]);

\assert($context instanceof Process);
assert($context instanceof Process);

// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
Expand All @@ -30,5 +31,5 @@

yield $context->join(); // Wait for child process to finish.

\printf("Final value of parcel: %d\n", yield $parcel->unwrap());
printf("Final value of parcel: %d\n", yield $parcel->unwrap());
});
14 changes: 7 additions & 7 deletions examples/thread.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__).'/vendor/autoload.php';
require dirname(__DIR__).'/vendor/autoload.php';

use Amp\Delayed;
use Amp\Loop;
Expand All @@ -17,28 +17,28 @@
try {
// Create a new child thread that does some blocking stuff.
$context = yield Thread::run(function (Channel $channel): \Generator {
\printf("Received the following from parent: %s\n", yield $channel->receive());
printf("Received the following from parent: %s\n", yield $channel->receive());

print "Sleeping for 3 seconds...\n";
\sleep(3); // Blocking call in thread.
sleep(3); // Blocking call in thread.

yield $channel->send("Data sent from child.");

print "Sleeping for 2 seconds...\n";
\sleep(2); // Blocking call in thread.
sleep(2); // Blocking call in thread.

return 42;
});

\assert($context instanceof Thread);
assert($context instanceof Thread);

print "Waiting 2 seconds to send start data...\n";
yield new Delayed(2000);

yield $context->send("Start data");

\printf("Received the following from child: %s\n", yield $context->receive());
\printf("Thread ended with value %d!\n", yield $context->join());
printf("Received the following from child: %s\n", yield $context->receive());
printf("Thread ended with value %d!\n", yield $context->join());
} finally {
Loop::cancel($timer);
}
Expand Down
11 changes: 6 additions & 5 deletions examples/threaded-parcel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__).'/vendor/autoload.php';

require dirname(__DIR__).'/vendor/autoload.php';

use Amp\Delayed;
use Amp\Loop;
Expand All @@ -17,19 +18,19 @@
return $value + 1;
});

\printf("Value after modifying in child thread: %s\n", $value);
printf("Value after modifying in child thread: %s\n", $value);

yield new Delayed(500); // Main thread should access parcel during this time.

// Unwrapping the parcel now should give value from main thread.
\printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap());
printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap());

yield $parcel->synchronized(function (int $value) {
return $value + 1;
});
}, $parcel);

\assert($context instanceof Thread);
assert($context instanceof Thread);

yield new Delayed(100); // Give the thread time to start and access the parcel.

Expand All @@ -39,5 +40,5 @@

yield $context->join(); // Wait for child thread to finish.

\printf("Final value of parcel: %d\n", yield $parcel->unwrap());
printf("Final value of parcel: %d\n", yield $parcel->unwrap());
});
4 changes: 2 additions & 2 deletions examples/worker-pool-simple.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

require \dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/vendor/autoload.php';

use Amp\Parallel\Worker;
use Amp\Promise;
Expand All @@ -20,5 +20,5 @@
$responses = Promise\wait(Promise\all($promises));

foreach ($responses as $url => $response) {
\printf("Read %d bytes from %s\n", \strlen($response), $url);
printf("Read %d bytes from %s\n", strlen($response), $url);
}
8 changes: 4 additions & 4 deletions examples/worker-pool.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

require \dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/vendor/autoload.php';

use Amp\Loop;
use Amp\Parallel\Worker\CallableTask;
Expand All @@ -20,7 +20,7 @@
// Event loop for parallel tasks
Loop::run(function () use (&$results, $tasks) {
$timer = Loop::repeat(200, function () {
\printf(".");
printf(".");
});
Loop::unreference($timer);

Expand All @@ -31,7 +31,7 @@
foreach ($tasks as $index => $task) {
$coroutines[] = Amp\call(function () use ($pool, $index, $task) {
$result = yield $pool->enqueue($task);
\printf("\nRead from task %d: %d bytes\n", $index, \strlen($result));
printf("\nRead from task %d: %d bytes\n", $index, strlen($result));
return $result;
});
}
Expand All @@ -42,4 +42,4 @@
});

echo "\nResult array keys:\n";
echo \var_export(\array_keys($results), true);
echo var_export(array_keys($results), true);
7 changes: 4 additions & 3 deletions examples/worker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
require \dirname(__DIR__) . '/vendor/autoload.php';

require dirname(__DIR__) . '/vendor/autoload.php';

use Amp\Parallel\Worker\CallableTask;
use Amp\Parallel\Worker\DefaultWorkerFactory;
Expand All @@ -11,8 +12,8 @@
$worker = $factory->create();

$result = yield $worker->enqueue(new CallableTask('file_get_contents', ['https://google.com']));
\printf("Read %d bytes\n", \strlen($result));
printf("Read %d bytes\n", strlen($result));

$code = yield $worker->shutdown();
\printf("Code: %d\n", $code);
printf("Code: %d\n", $code);
});
3 changes: 0 additions & 3 deletions lib/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

interface Context extends Channel
{
/**
* @return bool
*/
public function isRunning(): bool;

/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Context/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface ContextFactory
*
* @param string|string[] $script Path to PHP script or array with first element as path and following elements options
* to the PHP script (e.g.: ['bin/worker', 'Option1Value', 'Option2Value'].
*
* @return Context
*/
public function create($script): Context;

Expand Down
4 changes: 0 additions & 4 deletions lib/Context/Internal/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ public function kill()
}

/**
* @param \Amp\Parallel\Sync\Channel $channel
*
* @return \Generator
*
* @codeCoverageIgnore Only executed in thread.
*/
private function execute(Channel $channel): \Generator
Expand Down
4 changes: 1 addition & 3 deletions lib/Context/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function start(): Promise
}

return 0;
// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd
}, [
$this->id,
$this->hub->getUri(),
Expand Down Expand Up @@ -403,8 +403,6 @@ public function send($data): Promise
/**
* Returns the ID of the thread. This ID will be unique to this process.
*
* @return int
*
* @throws \Amp\Process\StatusError
*/
public function getId(): int
Expand Down
Loading

0 comments on commit 94a6590

Please sign in to comment.