9 Commits

73 changed files with 322 additions and 3552 deletions

7
.gitignore vendored
View File

@@ -7,6 +7,7 @@
# ----------------------------------------------------------------------------------------------------------------------
### Composer
# ----------------------------------------------------------------------------------------------------------------------
/composer.lock
/composer.phar
@@ -28,6 +29,12 @@
/.php_cs.cache
# ----------------------------------------------------------------------------------------------------------------------
### Build files
# ----------------------------------------------------------------------------------------------------------------------
/build/
# ----------------------------------------------------------------------------------------------------------------------
### Generated databases
# ----------------------------------------------------------------------------------------------------------------------

View File

@@ -2,7 +2,7 @@
"name": "meritoo/common-library",
"description": "Useful classes, methods, extensions etc.",
"license": "MIT",
"version": "0.0.12",
"version": "0.0.14",
"authors": [
{
"name": "Meritoo.pl",
@@ -17,18 +17,22 @@
"symfony/http-foundation": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^2.9",
"phpmd/phpmd": "^2.6",
"sebastian/phpcpd": "^3.0",
"friendsofphp/php-cs-fixer": "^2.6",
"pdepend/pdepend": "^2.5",
"phploc/phploc": "^4.0",
"friendsofphp/php-cs-fixer": "^2.6"
"phpmd/phpmd": "^2.6",
"phpunit/phpunit": "^5.7",
"sebastian/phpcpd": "^3.0",
"squizlabs/php_codesniffer": "^2.9"
},
"autoload": {
"psr-4": {
"Meritoo\\Common\\": "src/Meritoo/Common/",
"Meritoo\\Common\\Test\\": "tests/Meritoo/Common/Test/"
"Meritoo\\Common\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Meritoo\\Common\\Test\\": "tests/"
}
}
}

3494
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
/**
* Useful arrays methods
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Arrays
{

View File

@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
/**
* Useful methods for bundle
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Bundle
{

View File

@@ -16,8 +16,8 @@ use Meritoo\Common\Type\DatePartType;
/**
* Useful date methods
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Date
{

View File

@@ -14,8 +14,8 @@ use DateTime;
* A date's period.
* Contains start and end date of the period.
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class DatePeriod
{

View File

@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
/**
* Useful locale methods
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Locale
{

View File

@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
/**
* Useful methods for mime types of files
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MimeTypes
{

View File

@@ -15,8 +15,8 @@ use Transliterator;
/**
* Miscellaneous methods (only static functions)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Miscellaneous
{

View File

@@ -17,8 +17,8 @@ use Doctrine\ORM\QueryBuilder;
/**
* Useful methods for query builder (the Doctrine's QueryBuilder class)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class QueryBuilderUtility
{

View File

@@ -8,9 +8,9 @@
namespace Meritoo\Common\Utilities;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Util\Inflector;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
@@ -23,8 +23,8 @@ use ReflectionProperty;
/**
* Useful reflection methods
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Reflection
{
@@ -229,17 +229,29 @@ class Reflection
if ($class->hasProperty($property) || $force) {
$property = Inflector::classify($property);
$methodPrefixes = [
$getterPrefixes = [
'get',
'has',
'is',
];
foreach ($methodPrefixes as $prefix) {
$method = sprintf('%s%s', $prefix, $property);
foreach ($getterPrefixes as $prefix) {
$getterName = sprintf('%s%s', $prefix, $property);
if ($class->hasMethod($method)) {
$value = $object->{$method}();
if ($class->hasMethod($getterName)) {
$method = new ReflectionMethod($object, $getterName);
/*
* Getter is not accessible publicly?
* I have to skip it, to avoid an error like this:
*
* Call to protected method My\ExtraClass::getExtraProperty() from context 'My\ExtraClass'
*/
if ($method->isProtected() || $method->isPrivate()) {
continue;
}
$value = $object->{$getterName}();
$valueFound = true;
break;
}
@@ -248,13 +260,14 @@ class Reflection
if (!$valueFound && null !== $reflectionProperty) {
/*
* Oops, we have got exception.
* Oops, value of the property is still unknown
*
* 3rd try:
* Let's try modify accessibility of the property and try again to get value.
* Let's modify accessibility of the property and try again to get value
*/
$reflectionProperty->setAccessible(true);
$value = $reflectionProperty->getValue($object);
$reflectionProperty->setAccessible(false);
}
}
}
@@ -275,6 +288,14 @@ class Reflection
*/
public static function getPropertyValues($objects, $property, $force = false)
{
/*
* No objects?
* Nothing to do
*/
if (empty($objects)) {
return [];
}
if ($objects instanceof Collection) {
$objects = $objects->toArray();
}

View File

@@ -14,8 +14,8 @@ use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
/**
* Useful regular expressions methods
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Regex
{

View File

@@ -14,8 +14,8 @@ use Doctrine\ORM\QueryBuilder;
/**
* Useful methods for repository
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Repository
{

View File

@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
/**
* Useful uri methods (only static functions)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Uri
{

View File

@@ -15,8 +15,8 @@ use SimpleXMLElement;
/**
* Useful XML-related methods (only static functions)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Xml
{

View File

@@ -27,9 +27,9 @@ class TooManyChildClassesExceptionTest extends BaseTestCase
}
/**
* @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array
* @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array
* of objects, strings, object or string.
* @param array $childClasses Child classes
* @param array $childClasses Child classes
* @param string $expectedMessage Expected exception's message
*
* @dataProvider provideParentAndChildClasses

View File

@@ -25,7 +25,7 @@ class ArraysTest extends BaseTestCase
private $complexArray;
private $superComplexArray;
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Arrays::class);
}

View File

@@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\Bundle;
*/
class BundleTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Bundle::class);
}

View File

@@ -27,7 +27,7 @@ class ComposerTest extends BaseTestCase
*/
private $composerJsonPath;
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Composer::class);
}

View File

@@ -23,7 +23,7 @@ use Meritoo\Common\Utilities\Date;
*/
class DateTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Date::class);
}

View File

@@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\GeneratorUtility;
*/
class GeneratorUtilityTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(GeneratorUtility::class);
}

View File

@@ -20,7 +20,7 @@ use Meritoo\Common\Utilities\Locale;
*/
class LocaleTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Locale::class);
}

View File

@@ -20,7 +20,7 @@ use Meritoo\Common\Utilities\MimeTypes;
*/
class MimeTypesTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(MimeTypes::class);
}

View File

@@ -28,7 +28,7 @@ class MiscellaneousTest extends BaseTestCase
private $stringCommaSeparated;
private $stringDotSeparated;
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Miscellaneous::class);
}
@@ -159,8 +159,8 @@ class MiscellaneousTest extends BaseTestCase
$expected = "int(123)\n";
if ($xdebugLoaded) {
$libraryPath = realpath(sprintf('%s%s', dirname(__FILE__), '/../../../../..'));
$filePath = sprintf('%s%s', $libraryPath, '/src/Meritoo/Common/Utilities/Miscellaneous.php:');
$libraryPath = realpath(sprintf('%s%s', dirname(__FILE__), '/../..'));
$filePath = sprintf('%s%s', $libraryPath, '/src/Utilities/Miscellaneous.php:');
/*
* Attention. I have to use "\d+" at the end of $filePath, because number of line may be different if new

View File

@@ -0,0 +1,54 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The F class.
* Used for testing the Reflection class.
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class F
{
protected $username;
private $accountBalance;
private $city;
private $country;
private $gInstance;
public function __construct($accountBalance, $city, $country, $username, $firstName = 'John', $lastName = 'Scott')
{
$this->accountBalance = $accountBalance;
$this->city = $city;
$this->country = $country;
$this->username = $username;
$this->gInstance = new G($firstName, $lastName);
/*
* Called to avoid "Unused private method getAccountBalance" warning only
*/
$this->getAccountBalance();
}
public function getCountry()
{
return $this->country;
}
protected function getCity()
{
return $this->city;
}
private function getAccountBalance()
{
return $this->accountBalance;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Test\Utilities\Reflection;
/**
* The G class.
* Used for testing the Reflection class.
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class G
{
private $firstName;
private $lastName;
public function __construct($firstName = 'John', $lastName = 'Scott')
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}

View File

@@ -10,6 +10,7 @@ namespace Meritoo\Common\Test\Utilities;
use DateTime;
use Generator;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
@@ -19,6 +20,8 @@ use Meritoo\Common\Test\Utilities\Reflection\B;
use Meritoo\Common\Test\Utilities\Reflection\C;
use Meritoo\Common\Test\Utilities\Reflection\D;
use Meritoo\Common\Test\Utilities\Reflection\E;
use Meritoo\Common\Test\Utilities\Reflection\F;
use Meritoo\Common\Test\Utilities\Reflection\G;
use Meritoo\Common\Utilities\Reflection;
use ReflectionProperty;
@@ -30,7 +33,7 @@ use ReflectionProperty;
*/
class ReflectionTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Reflection::class);
}
@@ -239,6 +242,143 @@ class ReflectionTest extends BaseTestCase
self::assertCount(2, Reflection::getProperties(B::class, null, true));
}
public function testGetPropertyValueOfNotExistingProperty()
{
self::assertNull(Reflection::getPropertyValue(new D(), 'something'));
self::assertNull(Reflection::getPropertyValue(new D(), 'something', true));
}
public function testGetPropertyValueFromChain()
{
$f = new F(1000, 'New York', 'USA', 'john.scott');
self::assertEquals('John', Reflection::getPropertyValue($f, 'gInstance.firstName'));
}
public function testGetPropertyValueWithPublicGetter()
{
$country = 'USA';
$f = new F(1000, 'New York', $country, 'john.scott');
self::assertEquals($country, Reflection::getPropertyValue($f, 'country'));
}
public function testGetPropertyValueWithProtectedGetter()
{
$city = 'New York';
$f = new F(1000, $city, 'USA', 'john.scott');
self::assertEquals($city, Reflection::getPropertyValue($f, 'city'));
}
public function testGetPropertyValueWithPrivateGetter()
{
$accountBalance = 1000;
$f = new F($accountBalance, 'New York', 'USA', 'john.scott');
self::assertEquals($accountBalance, Reflection::getPropertyValue($f, 'accountBalance'));
}
public function testGetPropertyValueWithoutGetter()
{
$username = 'john.scott';
$f = new F(1000, 'New York', 'USA', $username);
self::assertEquals($username, Reflection::getPropertyValue($f, 'username'));
}
public function testGetPropertyValuesFromEmptySource()
{
self::assertEquals([], Reflection::getPropertyValues([], 'something'));
self::assertEquals([], Reflection::getPropertyValues(new Collection(), 'something'));
}
public function testGetPropertyValuesOfNotExistingPropertyFromSingleObject()
{
self::assertEquals([], Reflection::getPropertyValues(new D(), 'something'));
self::assertEquals([], Reflection::getPropertyValues(new D(), 'something', true));
}
public function testGetPropertyValuesOfNotExistingPropertyFromMultipleObjects()
{
$objects = [
new A(),
new A(),
new A(),
new B(),
new B(),
new C(),
new D(),
];
self::assertEquals([], Reflection::getPropertyValues($objects, 'something'));
self::assertEquals([], Reflection::getPropertyValues($objects, 'something', true));
$collection = new Collection($objects);
self::assertEquals([], Reflection::getPropertyValues($collection, 'something'));
self::assertEquals([], Reflection::getPropertyValues($collection, 'something', true));
}
public function testGetPropertyValuesOfExistingPropertyFromSingleObject()
{
self::assertEquals(['John'], Reflection::getPropertyValues(new G(), 'firstName'));
self::assertEquals(['John'], Reflection::getPropertyValues(new G(), 'firstName', true));
}
public function testGetPropertyValuesOfExistingPropertyFromMultipleObjects()
{
$expected = [
'New York',
'London',
'Tokyo',
];
$objects = [
new F(1000, 'New York', 'USA', 'john.scott'),
new F(2000, 'London', 'GB', 'john.scott'),
new F(3000, 'Tokyo', 'Japan', 'john.scott'),
];
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'city'));
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'city', true));
$collection = new Collection($objects);
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'city'));
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'city', true));
}
public function testGetPropertyValuesFromChainAndSingleObject()
{
$f = new F(1000, 'New York', 'USA', 'john.scott');
self::assertEquals(['John'], Reflection::getPropertyValues($f, 'gInstance.firstName'));
self::assertEquals(['John'], Reflection::getPropertyValues($f, 'gInstance.firstName', true));
}
public function testGetPropertyValuesFromChainAndMultipleObjects()
{
$expected = [
'John',
'Mary',
'Peter',
];
$objects = [
new F(1000, 'New York', 'USA', 'john.scott'),
new F(2000, 'London', 'GB', 'john.scott', 'Mary', 'Jane'),
new F(3000, 'Tokyo', 'Japan', 'john.scott', 'Peter', 'Brown'),
];
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'gInstance.firstName'));
self::assertEquals($expected, Reflection::getPropertyValues($objects, 'gInstance.firstName', true));
$collection = new Collection($objects);
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'gInstance.firstName'));
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'gInstance.firstName', true));
}
/**
* Provides invalid class and trait
*

View File

@@ -21,7 +21,7 @@ class RegexTest extends BaseTestCase
private $simpleText;
private $camelCaseText;
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Regex::class);
}

View File

@@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\Uri;
*/
class UriTest extends BaseTestCase
{
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Uri::class);
}

View File

@@ -23,7 +23,7 @@ class XmlTest extends BaseTestCase
private $simpleXml;
private $advancedXml;
public function verifyConstructor()
public function testConstructor()
{
static::assertHasNoConstructor(Xml::class);
}