Skip to content

Commit e3cbf3b

Browse files
authored
Upgrade to PHP 8.1 (#4)
1 parent 982d6d1 commit e3cbf3b

File tree

16 files changed

+43
-114
lines changed

16 files changed

+43
-114
lines changed

.github/workflows/build.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: "shivammathur/setup-php@v2"
1919
with:
2020
coverage: "pcov"
21-
php-version: "8.0"
21+
php-version: "8.1"
2222
ini-values: memory_limit=-1
2323

2424
- name: "Cache Composer dependencies"
@@ -27,8 +27,8 @@ jobs:
2727
path: |
2828
~/.composer/cache
2929
vendor
30-
key: "php-8.0"
31-
restore-keys: "php-8.0"
30+
key: "php-8.1"
31+
restore-keys: "php-8.1"
3232

3333
- name: "Validate composer"
3434
run: "composer validate"

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## [1.0.0] - 2022-05-22
8+
- Upgrade to PHP 8
9+
- Add php-cs-fixer
10+
- Add phpstan by @akondas in #1
11+
- Configure GitHub Actions by @akondas in #3
812

913
## [0.1.0] - 2018-03-15
1014
### Added
@@ -13,4 +17,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1317
- Storing and validate Blockchain
1418
- Proof of Work with difficulty (missing consensus on the difficulty)
1519
- Communicating with other nodes & controlling the node (based on ReactPHP)
16-

Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM php:8.1-cli-alpine
2+
RUN apk add --no-cache bash curl git
3+
RUN docker-php-ext-install pcntl
4+
COPY --from=composer:2.1.5 /usr/bin/composer /usr/bin/composer

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
build:
2+
docker build -t php-blockchain .
3+
4+
shell: build
5+
docker run -it --rm --name php-blockchain-dev -v $(PWD):/var/app -w /var/app php-blockchain:latest /bin/bash
6+
7+
.PHONY: build shell

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Blockchain implementation in PHP
22

3-
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
4-
[![Build Status](https://travis-ci.org/akondas/php-blockchain.svg?branch=master)](https://travis-ci.org/akondas/php-blockchain)
3+
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)
4+
[![Build](https://github.com/akondas/php-blockchain/actions/workflows/build.yaml/badge.svg)](https://github.com/akondas/php-blockchain/actions/workflows/build.yaml)
55
[![License](https://poser.pugx.org/akondas/php-blockchain/license.svg)](https://packagist.org/packages/akondas/php-blockchain)
66

77
Clean code approach to blockchain technology. Learn blockchain by reading source code.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.0",
13+
"php": "^8.1",
1414
"react/http": "^0.8.1",
1515
"friendsofphp/php-cs-fixer": "^3.8"
1616
},

composer.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Block.php

+8-50
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,15 @@ final class Block implements JsonSerializable
1111
{
1212
public const HASH_ALGORITHM = 'sha256';
1313

14-
/**
15-
* @var int
16-
*/
17-
private $index;
18-
19-
/**
20-
* @var string
21-
*/
22-
private $hash;
23-
24-
/**
25-
* @var string
26-
*/
27-
private $previousHash;
28-
29-
/**
30-
* @var \DateTimeImmutable
31-
*/
32-
private $createdAt;
33-
34-
/**
35-
* @var string
36-
*/
37-
private $data;
38-
39-
/**
40-
* @var int
41-
*/
42-
private $difficulty;
43-
44-
/**
45-
* @var int
46-
*/
47-
private $nonce;
48-
4914
public function __construct(
50-
int $index,
51-
string $hash,
52-
string $previousHash,
53-
DateTimeImmutable $createdAt,
54-
string $data,
55-
int $difficulty,
56-
int $nonce
15+
private int $index,
16+
private string $hash,
17+
private string $previousHash,
18+
private DateTimeImmutable $createdAt,
19+
private string $data,
20+
private int $difficulty,
21+
private int $nonce
5722
) {
58-
$this->index = $index;
59-
$this->hash = $hash;
60-
$this->previousHash = $previousHash;
61-
$this->createdAt = $createdAt;
62-
$this->data = $data;
63-
$this->difficulty = $difficulty;
64-
$this->nonce = $nonce;
6523
}
6624

6725
public static function genesis(): self
@@ -134,7 +92,7 @@ public static function calculateHash(
13492
}
13593

13694
/**
137-
* @return mixed[]
95+
* @return array<string,string|int>
13896
*/
13997
public function jsonSerialize(): array
14098
{

src/Blockchain.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function withLastBlockOnly(): self
5454
}
5555

5656
/**
57-
* @return Block[]
57+
* @return non-empty-array<Block>
5858
*/
5959
public function blocks(): array
6060
{

src/Miner.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,8 @@
99

1010
final class Miner
1111
{
12-
/**
13-
* @var Blockchain
14-
*/
15-
private $blockchain;
16-
17-
/**
18-
* @var HashDifficulty
19-
*/
20-
private $hashDifficulty;
21-
22-
public function __construct(Blockchain $blockchain, HashDifficulty $hashDifficulty)
12+
public function __construct(private Blockchain $blockchain, private HashDifficulty $hashDifficulty)
2313
{
24-
$this->blockchain = $blockchain;
25-
$this->hashDifficulty = $hashDifficulty;
2614
}
2715

2816
public function mineBlock(string $data): Block

src/Miner/HashDifficulty/ZeroPrefix.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function hashMatchesDifficulty(string $hash, int $difficulty): bool
1717
$binary = $this->binaryString($hash, $difficulty);
1818
$prefix = \str_repeat('0', $difficulty);
1919

20-
return \strpos($binary, $prefix) === 0;
20+
return \str_starts_with($binary, $prefix);
2121
}
2222

2323
private function binaryString(string $hash, int $difficulty): string

src/Node.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,8 @@
1010

1111
final class Node
1212
{
13-
/**
14-
* @var Miner
15-
*/
16-
private $miner;
17-
18-
/**
19-
* @var P2pServer
20-
*/
21-
private $p2pServer;
22-
23-
public function __construct(Miner $miner, P2pServer $p2pServer)
13+
public function __construct(private Miner $miner, private P2pServer $p2pServer)
2414
{
25-
$this->miner = $miner;
26-
$this->p2pServer = $p2pServer;
2715
}
2816

2917
/**

src/Node/Message.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ final class Message
1212

1313
public const BLOCKCHAIN = 'blockchain';
1414

15-
/**
16-
* @var string
17-
*/
18-
private $type;
19-
20-
/**
21-
* @var ?string
22-
*/
23-
private $data;
24-
25-
public function __construct(string $type, ?string $data = null)
26-
{
27-
$this->type = $type;
28-
$this->data = $data;
15+
public function __construct(
16+
private string $type,
17+
private ?string $data = null
18+
) {
2919
}
3020

3121
public function type(): string

src/Node/Peer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function port(): int
2929
}
3030

3131
/**
32-
* @return mixed[]
32+
* @return array<string,string|int>
3333
*/
3434
public function jsonSerialize(): array
3535
{

src/WebServer.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010

1111
final class WebServer
1212
{
13-
/**
14-
* @var Node
15-
*/
16-
private $node;
17-
18-
public function __construct(Node $node)
13+
public function __construct(private Node $node)
1914
{
20-
$this->node = $node;
2115
}
2216

2317
public function __invoke(ServerRequestInterface $request): Response

src/WebServer/Response/JsonResponse.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
final class JsonResponse extends HttpResponse implements Response
1111
{
12-
/**
13-
* @param mixed $data
14-
*/
15-
public function __construct($data)
12+
public function __construct(mixed $data)
1613
{
1714
parent::__construct(200, ['Content-Type' => 'application/json'], \json_encode($data));
1815
}

0 commit comments

Comments
 (0)