Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
- renamed test directory
- improve internal code quality
- using PHP7 strict type mode
- preparing 1.0.0 release
  • Loading branch information
nyamsprod committed Jan 4, 2017
1 parent 06f6a42 commit 66fb87e
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 368 deletions.
4 changes: 1 addition & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
/.php_cs export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/benchmarks export-ignore
/README.md export-ignore
/scrutinizer.yml export-ignore
/CHANGELOG.md export-ignore
/CONDUCT.md export-ignore
/phpunit.xml export-ignore
/test export-ignore
/tests export-ignore
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ return Symfony\CS\Config\Config::create()
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/test')
->in(__DIR__.'/tests')
);
6 changes: 3 additions & 3 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
filter:
paths: [src/*]
excluded_paths: [test/*]
excluded_paths: [tests/*]
checks:
php:
code_rating: true
Expand Down Expand Up @@ -29,7 +29,7 @@ tools:
paths: ['src']
php_loc:
enabled: true
excluded_dirs: [test, vendor]
excluded_dirs: [tests, vendor]
php_cpd:
enabled: true
excluded_dirs: [test, vendor]
excluded_dirs: [tests, vendor]
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

All Notable changes to `league-uri-parser` will be documented in this file

## Next
## 1.0.0 - 2017-01-04

### Added

- None
- `League\Uri\Exception::createFromInvalidHostname`

### Fixed

- None
- `League\Uri\Parser::isHost` method improved

### Deprecated

Expand All @@ -19,6 +19,7 @@ All Notable changes to `league-uri-parser` will be documented in this file
### Removed

- Support for PHP5
- Benchmark test

## 0.3.0 - 2016-11-09

Expand Down
162 changes: 2 additions & 160 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,175 +24,17 @@ $ composer require league/uri-parser
Documentation
---------

This is a drop-in replacement to PHP's `parse_url` function, with the following differences:

- The parser is RFC3986 compliant

```php
<?php

use League\Uri\Parser;

$parser = new Parser();
var_export($parser('http://[email protected]/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'foo.com',
// 'port' => null,
// 'path' => '',
// 'query' => '@bar.com/',
// 'fragment' => null,
//);

var_export(parse_url('http://[email protected]/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'host' => 'bar.com',
// 'user' => 'foo.com?',
// 'path' => '/',
//);
```

- The `Parser::__invoke` method always returns all URI components.

```php
<?php

use League\Uri\Parser;

$parser = new Parser();
var_export($parser('http://www.example.com/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => 'www.example.com',
// 'port' => null,
// 'path' => '/',
// 'query' => null,
// 'fragment' => null,
//);

var_export(parse_url('http://www.example.com/'));
//returns the following array
//array(
// 'scheme' => 'http',
// 'host' => 'www.example.com',
// 'path' => '/',
//);
```

- Accessing individual component is simple without needing extra parameters:

```php
<?php

use League\Uri\Parser;

$uri = 'http://www.example.com/';
$parser = new Parser();
$parser($uri)['query']; //returns null
parse_url($uri, PHP_URL_QUERY); //returns null
```

- Empty component and undefined component are treated differently

A distinction is made between an unspecified component, which will be set to `null` and an empty component which will be equal to the empty string.

```php
<?php

use League\Uri\Parser;

$uri = 'http://www.example.com/?';
$parser = new Parser();
$parser($uri)['query']; //returns ''
parse_url($uri, PHP_URL_QUERY); //returns null
```

- The path component is never equal to `null`

Since a URI is made of at least a path component, this component is never equal to `null`

```php
<?php

use League\Uri\Parser;

$uri = 'http://www.example.com?';
$parser = new Parser();
$parser($uri)['path']; //returns ''
parse_url($uri, PHP_URL_PATH); //returns null
```

- On malformed URI, the parser will throw a `ParserException` exception which extends SPL `InvalidArgumentException` instead of returning `false`.

```php
<?php

use League\Uri\Parser;

$uri = '//user@:80';
$parser = new Parser();
$parser($uri);
//throw a ParserException

parse_url($uri); //returns false
```

- Just like `parse_url`, the `League\Uri\Parser` only parses and extracts from the URI string its components. **You still need to validate them against its scheme specific rules.**

```php
<?php

use League\Uri\Parser;

$uri = 'http:www.example.com';
$parser = new Parser();
var_export($parser($uri));
//returns the following array
//array(
// 'scheme' => 'http',
// 'user' => null,
// 'pass' => null,
// 'host' => null,
// 'port' => null,
// 'path' => 'www.example.com',
// 'query' => null,
// 'fragment' => null,
//);
```

**This invalid HTTP URI is successfully parsed.**
Full documentation can be found at [uri.thephpleague.com](http://uri.thephpleague.com).

Testing
-------

`URI Parser` has:

- a [PHPUnit](https://phpunit.de) test suite.
- a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/).

To run the tests, run the following command from the project folder.
`League Uri Parser` has a [PHPUnit](https://phpunit.de) test suite and a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/). To run the tests, run the following command from the project folder.

```bash
$ composer test
```

Benchmark
-------

Additionally, a benchmark test suite using [PHP Bench](https://github.com/phpbench/phpbench) is provided.

```bash
$ composer benchmarks
```

Contributing
-------

Expand Down
23 changes: 0 additions & 23 deletions benchmarks/ParserBench.php

This file was deleted.

5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@
},
"autoload-dev": {
"psr-4": {
"LeagueTest\\Uri\\": "test"
"LeagueTest\\Uri\\": "tests"
}
},
"scripts": {
"test": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run;",
"phpunit": "phpunit --coverage-text",
"phpcs": "php-cs-fixer fix -v --diff --dry-run;",
"benchmarks": "phpbench run benchmarks/ParserBench.php --iterations=20 --revs=100 --report=aggregate"
"phpcs": "php-cs-fixer fix -v --diff --dry-run;"
},
"extra": {
"branch-alias": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<testsuites>
<testsuite name="Uri Parser Test Suite">
<directory>test</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

Expand Down
26 changes: 20 additions & 6 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* @author Ignace Nyamagana Butera <[email protected]>
* @copyright 2016 Ignace Nyamagana Butera
* @license https://github.com/thephpleague/uri-parser/blob/master/LICENSE (MIT License)
* @version 0.2.0
* @version 1.0.0
* @link https://github.com/thephpleague/uri-parser/
*/
declare(strict_types=1);

namespace League\Uri;

use InvalidArgumentException;
Expand All @@ -31,7 +33,7 @@ class Exception extends InvalidArgumentException
*
* @return static
*/
public static function createFromInvalidCharacters($uri)
public static function createFromInvalidCharacters(string $uri)
{
return new static(sprintf('The submitted uri `%s` contains invalid characters', $uri));
}
Expand All @@ -43,7 +45,7 @@ public static function createFromInvalidCharacters($uri)
*
* @return static
*/
public static function createFromInvalidScheme($uri)
public static function createFromInvalidScheme(string $uri)
{
return new static(sprintf('The submitted uri `%s` contains an invalid scheme', $uri));
}
Expand All @@ -55,15 +57,27 @@ public static function createFromInvalidScheme($uri)
*
* @return static
*/
public static function createFromInvalidHost($host)
public static function createFromInvalidHost(string $host)
{
return new static(sprintf('The submitted host `%s` is invalid', $host));
}

/**
* Returns a new Instance from an error in port validation
*
* @param string $port
* @param string $hostname
*
* @return static
*/
public static function createFromInvalidHostname(string $hostname)
{
return new static(sprintf('The submitted hostname `%s` is invalid', $hostname));
}

/**
* Returns a new Instance from an error in port validation
*
* @param mixed $port
*
* @return static
*/
Expand All @@ -79,7 +93,7 @@ public static function createFromInvalidPort($port)
*
* @return static
*/
public static function createFromInvalidPath($uri)
public static function createFromInvalidPath(string $uri)
{
return new static(sprintf('The submitted uri `%s` contains an invalid path', $uri));
}
Expand Down
Loading

0 comments on commit 66fb87e

Please sign in to comment.