Skip to content

Improve development configurations, update changelog #11

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

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.idea
/.phpunit.result.cache
/composer.phar
/vendor/
/coverage.xml
/phpcs.xml
/phpunit.xml
.phpunit.result.cache
/vendor/
28 changes: 11 additions & 17 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
filter:
paths: [src/*]
build:
environment:
php: 7.3.0
nodes:
analysis:
tests:
override:
- php-scrutinizer-run

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
filter:
paths:
- src/
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ language: php

cache:
directories:
- "$HOME/.composer/cache"
- vendor
- $HOME/.composer/cache

php:
- 7.3
- 7.4
- 8.0
- nightly
- hhvm


matrix:
jobs:
allow_failures:
- php: nightly
- php: hhvm
fast_finish: true

notifications:
Expand All @@ -34,5 +32,5 @@ script:
- composer check-code
- if [ "$TRAVIS_PHP_VERSION" == "8.0" ]; then composer test-cov; else composer test; fi

after_script:
after_success:
- if [ "$TRAVIS_PHP_VERSION" == "8.0" ]; then composer test-cov-upload; fi
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c

## [Unreleased]
### Added
- Bump minimum PHP to 7.3 and support PHP 8. (@MyZik) (#8)
### Changed
- Bump development dependencies, update tests.
- Improve all development configurations.
### Deprecated
### Removed
### Fixed
### Security
- Minimum PHP 7.3. (@MyZik) (#8)

## [0.1.0] - 2017-09-08
### Added
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
## Installation

### Composer

```bash
composer require php-telegram-bot/inline-keyboard-pagination:^1.0.0
composer require php-telegram-bot/inline-keyboard-pagination
```

## Usage

### Test Data

```php
$items = range(1, 100); // required.
$command = 'testCommand'; // optional. Default: pagination
$items = range(1, 100); // required.
$command = 'testCommand'; // optional. Default: pagination
$selectedPage = 10; // optional. Default: 1
$labels = [ // optional. Change button labels (showing defaults)
$labels = [ // optional. Change button labels (showing defaults)
'default' => '%d',
'first' => '« %d',
'previous' => '‹ %d',
Expand All @@ -44,6 +46,7 @@ $callbackDataFormat = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}'
```

### How To Use

```php
// Define inline keyboard pagination.
$ikp = new InlineKeyboardPagination($items, $command);
Expand All @@ -68,7 +71,7 @@ if (!empty($pagination['keyboard'])) {
//$pagination['keyboard'][1]['callback_data']; // command=testCommand&oldPage=10&newPage=7

...
$data['reply_markup' => [
$data['reply_markup'] = [
'inline_keyboard' => [
$pagination['keyboard'],
],
Expand All @@ -78,6 +81,7 @@ if (!empty($pagination['keyboard'])) {
```

To get the callback data, you can use the provided helper method (only works when using the default callback data format):

```php
// e.g. Callback data.
$callback_data = 'command=testCommand&oldPage=10&newPage=1';
Expand All @@ -96,7 +100,7 @@ parse_str($callbackData, $params);

## Code Quality

Run the PHPUnit tests via Composer script.
Run the PHPUnit tests via Composer script.

```bash
composer test
Expand All @@ -118,7 +122,7 @@ Project based on [Telegram Bot Pagination][github-lartie-tbp] by [lartie][github
[code-coverage-badge]: https://img.shields.io/codecov/c/github/php-telegram-bot/inline-keyboard-pagination.svg
[code-coverage]: https://codecov.io/gh/php-telegram-bot/inline-keyboard-pagination "Code coverage on Codecov"
[build-status-badge]: https://img.shields.io/travis/php-telegram-bot/inline-keyboard-pagination.svg
[build-status]: https://travis-ci.org/php-telegram-bot/inline-keyboard-pagination "Build status on Travis-CI"
[build-status]: https://travis-ci.com/php-telegram-bot/inline-keyboard-pagination "Build status on Travis-CI"

[latest-version-badge]: https://img.shields.io/packagist/v/php-telegram-bot/inline-keyboard-pagination.svg
[total-downloads-badge]: https://img.shields.io/packagist/dt/php-telegram-bot/inline-keyboard-pagination.svg
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
"php": "^7.3|^8.0"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "^0.9.2",
"php-parallel-lint/php-parallel-lint": "v1.2.0",
"phpunit/phpunit": "^9.5.2",
"squizlabs/php_codesniffer": "^3.0"
"php-parallel-lint/php-parallel-lint": "v1.2",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
Expand All @@ -45,13 +44,14 @@
"scripts": {
"check-code": [
"vendor/bin/parallel-lint . --exclude vendor",
"vendor/bin/phpcs src/ tests/ -snp --standard=psr2 --encoding=utf-8 --report-width=150"
"vendor/bin/phpcs"
],
"test": [
"vendor/bin/phpunit"
],
"test-cov": [
"vendor/bin/phpunit --coverage-clover=clover.xml"
"@putenv XDEBUG_MODE=coverage",
"vendor/bin/phpunit --coverage-clover coverage.xml"
],
"test-cov-upload": [
"curl -s https://codecov.io/bash | bash"
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

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

16 changes: 16 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">
<description>PHP Code Sniffer</description>

<arg value="snp"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="encoding" value="utf-8"/>
<arg name="parallel" value="8"/>
<arg name="report-width" value="150"/>

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

<rule ref="PSR12"/>
</ruleset>
29 changes: 10 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutOutputDuringTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix=".php">./src/Exception</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Inline Keyboard Pagination Test Suite">
<directory suffix="Test.php">./tests</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
42 changes: 20 additions & 22 deletions src/InlineKeyboardPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function setItems(array $items): InlineKeyboardPagination
*/
public function getNumberOfPages(): int
{
return (int)ceil(count($this->items) / $this->itemsPerPage);
return (int) ceil(count($this->items) / $this->itemsPerPage);
}

/**
Expand Down Expand Up @@ -309,31 +309,28 @@ protected function generateRange(): array
$numberOfIntermediateButtons = $this->maxButtons - 2;
$numberOfPages = $this->getNumberOfPages();

// @todo: Find a nicer solution for page 3
$from = $this->selectedPage - 1;
$to = $this->selectedPage + ($this->selectedPage === 3 ? $numberOfIntermediateButtons - 1 : 2);

if ($this->selectedPage === 1) {
$from = 2;
$to = $this->maxButtons;
} elseif ($this->selectedPage === $numberOfPages) {
$from = $numberOfPages - $numberOfIntermediateButtons;
$to = $numberOfPages;
} else {
if ($this->selectedPage < 3) {
$from = $this->selectedPage;
$to = $this->selectedPage + $numberOfIntermediateButtons;
} elseif (($numberOfPages - $this->selectedPage) < 3) {
$from = $numberOfPages - $numberOfIntermediateButtons;
$to = $numberOfPages;
} else {
// @todo: Find a nicer solution for page 3
if ($this->forceButtonCount) {
$from = $this->selectedPage - floor($numberOfIntermediateButtons / 2);
$to = $this->selectedPage + ceil(
$numberOfIntermediateButtons / 2
) + ($this->selectedPage === 3 && $this->maxButtons > 5);
} else {
$from = $this->selectedPage - 1;
$to = $this->selectedPage + ($this->selectedPage === 3 ? $numberOfIntermediateButtons - 1 : 2);
}
}
} elseif ($this->selectedPage < 3) {
$from = $this->selectedPage;
$to = $this->selectedPage + $numberOfIntermediateButtons;
} elseif (($numberOfPages - $this->selectedPage) < 3) {
$from = $numberOfPages - $numberOfIntermediateButtons;
$to = $numberOfPages;
} elseif ($this->forceButtonCount) {
$from = $this->selectedPage -
floor($numberOfIntermediateButtons / 2);
$to = $this->selectedPage +
ceil($numberOfIntermediateButtons / 2) +
(int) ($this->selectedPage === 3 && $this->maxButtons > 5);
}

return compact('from', 'to');
Expand All @@ -349,7 +346,7 @@ protected function generateRange(): array
protected function generateButton(int $page): array
{
return [
'text' => (string)$page,
'text' => (string) $page,
'callback_data' => $this->generateCallbackData($page),
];
}
Expand Down Expand Up @@ -393,10 +390,11 @@ protected function getOffset(): int
/**
* Get the parameters from the callback query.
*
* @todo Possibly make it work for custom formats too?
*
* @param string $data
*
* @return array
* @todo Possibly make it work for custom formats too?
*/
public static function getParametersFromCallbackData(string $data): array
{
Expand Down
Loading