Skip to content

Commit

Permalink
Refactoring (#12)
Browse files Browse the repository at this point in the history
* Переход на PHP 8.2

* refactoring

* datetime immutable

* fix github actions

* fix github actions

* add getDuration() into README

* changelog
  • Loading branch information
VitalyArt committed Nov 6, 2023
1 parent 584d335 commit 5877cec
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 187 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: PHPUnit
on: [push]
name: Tests
on: [ push ]

jobs:
PHPUnit:
runs-on: ubuntu-latest
strategy:
matrix:
PHP: [ "7.3", "7.4", "8.0", "8.1", "8.2" ]
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: 8.2
- name: PHPUnit Tests
uses: php-actions/phpunit@master
with:
configuration: test/phpunit/phpunit.xml
args: --testdox
version: 9
php_version: 8.2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/.idea

composer.lock
example.php

/vendor
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# v3.0.0

### Compatibility-breaking changes:
- PHP version has been upgraded to 8.2.
- The namespace `exceptions` has been renamed to `Exceptions`
- The file `bootstrap.php` has been removed.
- The `getTypeString()` method of the `Entry` class now returns an `EntryTypeEnum`.
- `DateTime` has been changed to `DateTimeImmutable`.

### Non-compatibility-breaking changes:
- DTO classes are now readonly.
- All parser exceptions now inherit from the `ParserException` class.
- The Demo class has added a `getDuration()` method that returns the duration of the demo file in seconds.
- An `EntryTypeEnum` enum has been added.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
composer-bash:
docker run --rm -it -v `pwd`/:/app composer:2 bash

composer-install:
docker run --rm -v `pwd`/:/app composer:2 composer install

Expand All @@ -20,5 +23,5 @@ run-php80-tests:
run-php81-tests:
docker run --rm -v `pwd`/:/app php:8.1 /app/vendor/phpunit/phpunit/phpunit --testdox --bootstrap /app/vendor/autoload.php /app/test/phpunit

run-php81-tests:
run-php82-tests:
docker run --rm -v `pwd`/:/app php:8.2 /app/vendor/phpunit/phpunit/phpunit --testdox --bootstrap /app/vendor/autoload.php /app/test/phpunit
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

This package is designed to obtain information from the demo of the servers or games on the Half-Life 1 engine.
Installation is possible in two versions:
1. Composer
To install with composer, either run
1. Install with composer, either run

```
$ php composer.phar require vitalyart/hltv-demo-parser "*"
Expand All @@ -22,14 +21,6 @@ or add

to the ```require``` section of your `composer.json` file.

2. Manual installation (Without composer)

Clone or download package from github and include bootstrap file

```
include '/path/to/hltv-demo-parser/src/bootstrap.php';
```

```php
$parser = new \VitalyArt\DemoParser\Parser();
$parser->setDemoFile('/path/to/demo/pub-1609152130-de_dust2_2x2.dem');
Expand All @@ -42,6 +33,7 @@ $demo->getMapName();
$demo->getClientName();
$demo->getStartTime();
$demo->getEndTime();
$demo->getDuration();

foreach($demo->getEntries() as $entry) {
$entry->getTypeString();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"GPL-3.0-or-later"
],
"require": {
"php": ">=7.3",
"php": "^8.2",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
48 changes: 21 additions & 27 deletions src/Demo.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser;

use DateTime;
use DateTimeImmutable;

class Demo
readonly class Demo
{
private $demoProtocol;
private $netProtocol;
private $mapName;
private $clientName;
private $entries;
private $startTime;
private $endTime;

public function __construct(
int $demoProtocol,
int $netProtocol,
string $mapName,
string $clientName,
array $entries,
?DateTime $startTime,
?DateTime $endTime
private int $demoProtocol,
private int $netProtocol,
private string $mapName,
private string $clientName,
private array $entries,
private DateTimeImmutable|null $startTime,
private DateTimeImmutable|null $endTime,
private int|false $duration,
)
{
$this->demoProtocol = $demoProtocol;
$this->netProtocol = $netProtocol;
$this->mapName = $mapName;
$this->clientName = $clientName;
$this->entries = $entries;
$this->startTime = $startTime;
$this->endTime = $endTime;

}

public function getDemoProtocol(): int
{
return $this->demoProtocol;
Expand All @@ -58,13 +47,18 @@ public function getEntries(): array
return $this->entries;
}

public function getStartTime(): ?DateTime
public function getStartTime(): DateTimeImmutable|null
{
return $this->startTime;
}

public function getEndTime(): ?DateTime
public function getEndTime(): DateTimeImmutable|null
{
return $this->endTime;
}

public function getDuration(): int|false
{
return $this->duration;
}
}
66 changes: 16 additions & 50 deletions src/Entry.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser;

class Entry
{
private $typeString;
private $type;
private $description;
private $flags;
private $CDTrack;
private $trackTime;
private $frames;
private $offset;
private $fileLength;
use VitalyArt\DemoParser\Enums\EntryTypeEnum;

/**
* @param string $typeString
* @param integer $type
* @param string $description
* @param integer $flags
* @param string $CDTrack
* @param float $trackTime
* @param integer $frames
* @param integer $offset
* @param integer $fileLength
*/
readonly class Entry
{
public function __construct(
string $typeString,
int $type,
string $description,
int $flags,
string $CDTrack,
float $trackTime,
int $frames,
int $offset,
int $fileLength
private EntryTypeEnum $typeString,
private int $type,
private string $description,
private int $flags,
private int $CDTrack,
private float $trackTime,
private int $frames,
private int $offset,
private int $fileLength,
)
{
$this->typeString = $typeString;
$this->type = $type;
$this->description = $description;
$this->flags = $flags;
$this->CDTrack = $CDTrack;
$this->trackTime = $trackTime;
$this->frames = $frames;
$this->offset = $offset;
$this->fileLength = $fileLength;

}

/**
* Entry type
* @return string
*/
public function getTypeString(): string
public function getTypeString(): EntryTypeEnum
{
return $this->typeString;
}

/**
* Integer entry type
* @return int
*/
public function getType(): int
{
Expand All @@ -68,7 +41,6 @@ public function getType(): int

/**
* Description
* @return string
*/
public function getDescription(): string
{
Expand All @@ -77,7 +49,6 @@ public function getDescription(): string

/**
* Flags
* @return int
*/
public function getFlags(): int
{
Expand All @@ -86,7 +57,6 @@ public function getFlags(): int

/**
* CD track
* @return string
*/
public function getCDTrack(): string
{
Expand All @@ -95,7 +65,6 @@ public function getCDTrack(): string

/**
* Track time
* @return float
*/
public function getTrackTime(): float
{
Expand All @@ -104,7 +73,6 @@ public function getTrackTime(): float

/**
* Frames
* @return int
*/
public function getFrames(): int
{
Expand All @@ -113,7 +81,6 @@ public function getFrames(): int

/**
* Offset
* @return int
*/
public function getOffset(): int
{
Expand All @@ -122,7 +89,6 @@ public function getOffset(): int

/**
* File length
* @return int
*/
public function getFileLength(): int
{
Expand Down
11 changes: 11 additions & 0 deletions src/Enums/EntryTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Enums;

enum EntryTypeEnum: string
{
case LOADING = 'loading';
case PLAYBACK = 'playback';
}
10 changes: 10 additions & 0 deletions src/Exceptions/FileNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class FileNotExistsException extends ParserException
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/FileNotSpecifiedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class FileNotSpecifiedException extends ParserException
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/IsNotADemoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class IsNotADemoException extends ParserException
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/NotReadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class NotReadableException extends ParserException
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/ParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class ParserException extends \Exception
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/WrongExtensionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace VitalyArt\DemoParser\Exceptions;

class WrongExtensionException extends ParserException
{

}
Loading

0 comments on commit 5877cec

Please sign in to comment.