Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPCS + PhpStan + badges #2

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7528840
Update composer.json
alexdnepro Jan 11, 2023
246bd7f
Add PHPCS
BrianHenryIE May 17, 2023
84718d0
Autofix `phpcbf`
BrianHenryIE May 17, 2023
67cf3bc
`const` -> `public const`
BrianHenryIE May 17, 2023
f8a40a4
phpcs line-length
BrianHenryIE May 17, 2023
d011f32
Add PhpStan
BrianHenryIE May 17, 2023
3d5b1d5
phsptan: return type
BrianHenryIE May 17, 2023
f699624
Fix `Unsafe usage of new static()`
BrianHenryIE May 17, 2023
6a84bbc
property is not covariant with PHPDoc
BrianHenryIE May 17, 2023
63194e3
PhpStan level 6 fixes
BrianHenryIE May 17, 2023
be5e4f0
PhpStan level 7 fixes
BrianHenryIE May 17, 2023
1b1e9fb
fix: phpcs line length
BrianHenryIE May 17, 2023
679d473
Add PHP code coverage exclusions to linting
BrianHenryIE May 17, 2023
b64cd0d
Add README header, coverage badge, coverage workflow
BrianHenryIE May 17, 2023
5550724
Move packages to `require-dev`
BrianHenryIE Nov 15, 2023
c1945ea
Add PHPCS
BrianHenryIE May 17, 2023
232ddb8
Add PhpStan
BrianHenryIE May 17, 2023
c15714d
Add PHP code coverage exclusions to linting
BrianHenryIE May 17, 2023
bea7c6e
Add README header, coverage badge, coverage workflow
BrianHenryIE May 17, 2023
31b610d
Move packages to `require-dev`
BrianHenryIE Nov 15, 2023
4ad0b26
`phpcbf` autofix
BrianHenryIE Nov 15, 2023
1abbadb
Merge branch 'master' into updates
BrianHenryIE Nov 15, 2023
daa998a
Fix phpcs "Visibility must be declared on all constants"
BrianHenryIE Nov 15, 2023
bed661a
Fix phpcs "Line exceeds 120 characters"
BrianHenryIE Nov 15, 2023
0726eca
fix phpcs
BrianHenryIE Nov 15, 2023
f8c2d88
Fix phpstan level 2
BrianHenryIE Nov 15, 2023
aa24423
Fix phpstan level 3
BrianHenryIE Nov 15, 2023
2b5c50e
Fix phpstan level 6
BrianHenryIE Nov 15, 2023
fc88c5e
Fix phpstan level 7
BrianHenryIE Nov 15, 2023
ce73f3c
Update phpstan.neon
BrianHenryIE Nov 15, 2023
4df90b3
Add test-coverage scripts
BrianHenryIE Nov 15, 2023
15287ed
Update coverage.svg
BrianHenryIE Nov 15, 2023
fc6f927
Fix "PHP 7.1" (was .4) in README
BrianHenryIE Nov 15, 2023
f60a820
Merge remote-tracking branch 'upstream/master'
BrianHenryIE Nov 16, 2023
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
21 changes: 21 additions & 0 deletions .github/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions .github/workflows/codecoverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Code Coverage

# Runs PHPUnit with code coverage enabled, commits the html report to
# GitHub Pages, generates a README badge with the coverage percentage.
#
# Requires a gh-pages branch already created.
#
# git checkout --orphan gh-pages
# touch index.html
# git add index.html
# git commit -m 'Set up gh-pages branch' index.html
# git push origin gh-pages
# git checkout master
#
# @author BrianHenryIE

on:
push:
branches:
- master

jobs:
tests:
runs-on: ubuntu-latest

steps:

- name: Git checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: xdebug

# - name: Checkout GitHub Pages branch for code coverage report
# uses: actions/checkout@v2
# with:
# ref: gh-pages
# path: tests/_reports/html

- name: Install dependencies
run: composer install --prefer-dist --no-suggest --no-progress

# - name: Clear previous code coverage
# run: |
# cd tests/_reports/html
# rm -rf *
# cd ../../..

- name: Run tests with code coverage
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover tests/_reports/clover.xml --coverage-html tests/_reports/html

# - name: Edit phpcov html output to work with gh-pages
# run: |
# cd tests/_reports/html
# mv _css css; find . -depth -name '*.html' -exec sed -i "s/_css\//css\//" {} +
# mv _icons icons; find . -depth -name '*.html' -exec sed -i "s/_icons\//icons\//" {} +
# mv _js js; find . -depth -name '*.html' -exec sed -i "s/_js\//js\//" {} +
# git add *
# cd ../../..

# - name: Commit code coverage to gh-pages
# uses: stefanzweifel/git-auto-commit-action@v4.1.1
# with:
# repository: tests/_reports/html
# branch: gh-pages
# commit_message: "🤖 Commit code coverage to gh-pages"
# commit_options:
# env:
# GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

- name: Update README badge
run: vendor/bin/php-coverage-badger tests/_reports/clover.xml .github/coverage.svg

- name: Commit code coverage badge
uses: stefanzweifel/git-auto-commit-action@v4.1.1
with:
commit_message: "🤖 Commit code coverage badge"


3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.phpunit.*
/vendor
composer.lock
composer.lock
tests/_reports/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![PHP 7.1](https://img.shields.io/badge/PHP-7.1-8892BF.svg)]() [![PHPCS PSR-12](https://img.shields.io/badge/PHPCS-PSR–12-226146.svg)](https://www.php-fig.org/psr/psr-12/) [![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/bh-php-monero-explorer/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%208-2a5ea7.svg)](https://phpstan.org/)

## PHP Bitcoin Address

A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH, P2TR output script/address parser/generator/validator.
Expand Down
24 changes: 23 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"shanecurran/phpecc": "^0.0.1"
},
"require-dev": {
"phpunit/phpunit": ">=5.7"
"dealerdirect/phpcodesniffer-composer-installer": "*",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": ">=5.7",
"squizlabs/php_codesniffer": "*"
},
"autoload": {
"psr-4": {
Expand All @@ -33,5 +36,24 @@
"psr-4": {
"AndKom\\Bitcoin\\Address\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"test": [
"phpunit"
],
"test-coverage":[
"phpunit --coverage-text --coverage-clover tests/_reports/clover.xml --coverage-html tests/_reports/html"
],
"lint": [
"phpcbf || true",
"phpcs || true",
"phpstan"
]
}
}
14 changes: 14 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">

<description>PHPCS configuration file.</description>

<file>src</file>
<file>tests</file>

<rule ref="PSR12"/>

<exclude-pattern>tests/_reports</exclude-pattern>
<exclude-pattern>tests/_data</exclude-pattern>

</ruleset>
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 9
paths:
- src
- tests
excludePaths:
- tests/_reports
treatPhpDocTypesAsCertain: false
7 changes: 6 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
16 changes: 10 additions & 6 deletions src/Ecc/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Point extends \Mdanter\Ecc\Primitives\Point
{
/**
* @return $this
* @return Point
* @throws Exception
*/
public function liftX(): self
Expand Down Expand Up @@ -50,24 +50,28 @@ public function liftX(): self
$curve->getPrime()
);

if ($adapter->cmp(
if (
$adapter->cmp(
$c,
$adapter->powmod(
$y,
gmp_init(2, 10),
$curve->getPrime()
)
) !== 0) {
) !== 0
) {
throw new Exception('C is not equal to point Y^2.');
}

if ($adapter->cmp(
if (
$adapter->cmp(
$adapter->mod(
$y,
gmp_init(2, 10)
),
gmp_init(0, 10)
) === 0) {
) === 0
) {
$point = new Point($adapter, $curve, $x, $y);
} else {
$point = new Point($adapter, $curve, $x, $adapter->sub($curve->getPrime(), $y));
Expand All @@ -91,6 +95,6 @@ static function fromPubKey(GmpMathInterface $adapter, CurveFpInterface $curve, s
$y = $curve->recoverYfromX($prefix === '03', $x);
}

return new static($adapter, $curve, $x, $y);
return new Point($adapter, $curve, $x, $y);
}
}
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
*/
class Exception extends \Exception
{
}
}
36 changes: 21 additions & 15 deletions src/Network/NetworkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
class NetworkFactory
{
/**
* @var array
* @var string[]
*/
static protected $networks = [
protected static $networks = [
'bitcoin',
'bitcoinTestnet',
'bitcoinCash',
Expand All @@ -49,27 +49,27 @@ class NetworkFactory
/**
* @var NetworkInterface
*/
static protected $defaultNetwork;
protected static $defaultNetwork;

/**
* @param NetworkInterface $network
*/
static public function setDefaultNetwork(NetworkInterface $network)
public static function setDefaultNetwork(NetworkInterface $network): void
{
static::$defaultNetwork = $network;
}

/**
* @return NetworkInterface
*/
static public function getDefaultNetwork(): NetworkInterface
public static function getDefaultNetwork(): NetworkInterface
{
return static::$defaultNetwork ?: static::bitcoin();
}

/**
* @param string $name
* @param array $arguments
* @param array<mixed> $arguments
* @return NetworkInterface
* @throws Exception
*/
Expand All @@ -81,27 +81,33 @@ public function createNetwork(string $name, array $arguments): NetworkInterface

$class = 'AndKom\\Bitcoin\\Address\\Network\\Networks\\' . ucfirst($name);

return new $class(...$arguments);
$network = new $class(...$arguments);

if (! $network instanceof NetworkInterface) {
throw new Exception("{$class} was not a NetworkInterface");
}

return $network;
}

/**
* @param $name
* @param $arguments
* @param string $name
* @param array<mixed> $arguments
* @return NetworkInterface
* @throws Exception
*/
public function __call($name, $arguments)
public function __call(string $name, array $arguments)
{
return $this->createNetwork($name, $arguments);
}

/**
* @param $name
* @param $arguments
* @param string $name
* @param array<mixed> $arguments
* @return NetworkInterface
*/
public static function __callStatic($name, $arguments)
public static function __callStatic(string $name, array $arguments)
{
return (new static)->createNetwork($name, $arguments);
return (new NetworkFactory())->createNetwork($name, $arguments);
}
}
}
2 changes: 1 addition & 1 deletion src/Network/NetworkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ public function decodeAddress(string $address): OutputInterface;
* @return bool
*/
public function validateAddress(string $address): bool;
}
}
2 changes: 1 addition & 1 deletion src/Network/Networks/Bitcoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class Bitcoin extends BitcoinAbstract
* @var bool
*/
protected $hasTaproot = true;
}
}
8 changes: 5 additions & 3 deletions src/Network/Networks/BitcoinAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use AndKom\Bitcoin\Address\Utils;
use AndKom\Bitcoin\Address\Validate;
use BrooksYang\Bech32m\Exception\Bech32mException;

use function BrooksYang\Bech32m\decodeSegwit;
use function BrooksYang\Bech32m\encodeSegwit;

use const BrooksYang\Bech32m\BECH32;
use const BrooksYang\Bech32m\BECH32M;

Expand All @@ -26,8 +28,8 @@
*/
abstract class BitcoinAbstract implements NetworkInterface
{
const VERSION_SEGWIT = 0;
const VERSION_TAPROOT = 1;
public const VERSION_SEGWIT = 0;
public const VERSION_TAPROOT = 1;

/**
* @var string
Expand Down Expand Up @@ -185,4 +187,4 @@ public function validateAddress(string $address): bool
return false;
}
}
}
}
Loading