Skip to content

Commit

Permalink
Upgrade to PHP 8.1 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored May 22, 2022
1 parent 982d6d1 commit e3cbf3b
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 114 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "8.0"
php-version: "8.1"
ini-values: memory_limit=-1

- name: "Cache Composer dependencies"
Expand All @@ -27,8 +27,8 @@ jobs:
path: |
~/.composer/cache
vendor
key: "php-8.0"
restore-keys: "php-8.0"
key: "php-8.1"
restore-keys: "php-8.1"

- name: "Validate composer"
run: "composer validate"
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

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

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

4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM php:8.1-cli-alpine
RUN apk add --no-cache bash curl git
RUN docker-php-ext-install pcntl
COPY --from=composer:2.1.5 /usr/bin/composer /usr/bin/composer
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build:
docker build -t php-blockchain .

shell: build
docker run -it --rm --name php-blockchain-dev -v $(PWD):/var/app -w /var/app php-blockchain:latest /bin/bash

.PHONY: build shell
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Blockchain implementation in PHP

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

Clean code approach to blockchain technology. Learn blockchain by reading source code.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"react/http": "^0.8.1",
"friendsofphp/php-cs-fixer": "^3.8"
},
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 8 additions & 50 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,15 @@ final class Block implements JsonSerializable
{
public const HASH_ALGORITHM = 'sha256';

/**
* @var int
*/
private $index;

/**
* @var string
*/
private $hash;

/**
* @var string
*/
private $previousHash;

/**
* @var \DateTimeImmutable
*/
private $createdAt;

/**
* @var string
*/
private $data;

/**
* @var int
*/
private $difficulty;

/**
* @var int
*/
private $nonce;

public function __construct(
int $index,
string $hash,
string $previousHash,
DateTimeImmutable $createdAt,
string $data,
int $difficulty,
int $nonce
private int $index,
private string $hash,
private string $previousHash,
private DateTimeImmutable $createdAt,
private string $data,
private int $difficulty,
private int $nonce
) {
$this->index = $index;
$this->hash = $hash;
$this->previousHash = $previousHash;
$this->createdAt = $createdAt;
$this->data = $data;
$this->difficulty = $difficulty;
$this->nonce = $nonce;
}

public static function genesis(): self
Expand Down Expand Up @@ -134,7 +92,7 @@ public static function calculateHash(
}

/**
* @return mixed[]
* @return array<string,string|int>
*/
public function jsonSerialize(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Blockchain.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function withLastBlockOnly(): self
}

/**
* @return Block[]
* @return non-empty-array<Block>
*/
public function blocks(): array
{
Expand Down
14 changes: 1 addition & 13 deletions src/Miner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@

final class Miner
{
/**
* @var Blockchain
*/
private $blockchain;

/**
* @var HashDifficulty
*/
private $hashDifficulty;

public function __construct(Blockchain $blockchain, HashDifficulty $hashDifficulty)
public function __construct(private Blockchain $blockchain, private HashDifficulty $hashDifficulty)
{
$this->blockchain = $blockchain;
$this->hashDifficulty = $hashDifficulty;
}

public function mineBlock(string $data): Block
Expand Down
2 changes: 1 addition & 1 deletion src/Miner/HashDifficulty/ZeroPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function hashMatchesDifficulty(string $hash, int $difficulty): bool
$binary = $this->binaryString($hash, $difficulty);
$prefix = \str_repeat('0', $difficulty);

return \strpos($binary, $prefix) === 0;
return \str_starts_with($binary, $prefix);
}

private function binaryString(string $hash, int $difficulty): string
Expand Down
14 changes: 1 addition & 13 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@

final class Node
{
/**
* @var Miner
*/
private $miner;

/**
* @var P2pServer
*/
private $p2pServer;

public function __construct(Miner $miner, P2pServer $p2pServer)
public function __construct(private Miner $miner, private P2pServer $p2pServer)
{
$this->miner = $miner;
$this->p2pServer = $p2pServer;
}

/**
Expand Down
18 changes: 4 additions & 14 deletions src/Node/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ final class Message

public const BLOCKCHAIN = 'blockchain';

/**
* @var string
*/
private $type;

/**
* @var ?string
*/
private $data;

public function __construct(string $type, ?string $data = null)
{
$this->type = $type;
$this->data = $data;
public function __construct(
private string $type,
private ?string $data = null
) {
}

public function type(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Peer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function port(): int
}

/**
* @return mixed[]
* @return array<string,string|int>
*/
public function jsonSerialize(): array
{
Expand Down
8 changes: 1 addition & 7 deletions src/WebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@

final class WebServer
{
/**
* @var Node
*/
private $node;

public function __construct(Node $node)
public function __construct(private Node $node)
{
$this->node = $node;
}

public function __invoke(ServerRequestInterface $request): Response
Expand Down
5 changes: 1 addition & 4 deletions src/WebServer/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

final class JsonResponse extends HttpResponse implements Response
{
/**
* @param mixed $data
*/
public function __construct($data)
public function __construct(mixed $data)
{
parent::__construct(200, ['Content-Type' => 'application/json'], \json_encode($data));
}
Expand Down

0 comments on commit e3cbf3b

Please sign in to comment.