mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Minor refactoring
This commit is contained in:
@@ -563,21 +563,22 @@ class Miscellaneous
|
|||||||
* @param string $suffix (optional) The suffix to add at the end of string
|
* @param string $suffix (optional) The suffix to add at the end of string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function substringToWord($text, $maxLength, $suffix = '...')
|
public static function substringToWord(string $text, int $maxLength, string $suffix = '...'): string
|
||||||
{
|
{
|
||||||
$effect = $text;
|
$effect = $text;
|
||||||
|
$encoding = 'utf-8';
|
||||||
|
|
||||||
$textLength = mb_strlen($text, 'utf-8');
|
$textLength = mb_strlen($text, $encoding);
|
||||||
$suffixLength = mb_strlen($suffix, 'utf-8');
|
$suffixLength = mb_strlen($suffix, $encoding);
|
||||||
|
|
||||||
$maxLength -= $suffixLength;
|
$maxLength -= $suffixLength;
|
||||||
|
|
||||||
if ($textLength > $maxLength) {
|
if ($textLength > $maxLength) {
|
||||||
$effect = mb_substr($text, 0, $maxLength, 'utf-8');
|
$effect = mb_substr($text, 0, $maxLength, $encoding);
|
||||||
$lastSpacePosition = mb_strrpos($effect, ' ', 0, 'utf-8');
|
$lastSpacePosition = mb_strrpos($effect, ' ', 0, $encoding);
|
||||||
|
|
||||||
if (false !== $lastSpacePosition) {
|
if (false !== $lastSpacePosition) {
|
||||||
$effect = mb_substr($effect, 0, $lastSpacePosition, 'utf-8');
|
$effect = mb_substr($effect, 0, $lastSpacePosition, $encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
$effect .= $suffix;
|
$effect .= $suffix;
|
||||||
|
|||||||
@@ -502,7 +502,7 @@ class Reflection
|
|||||||
*
|
*
|
||||||
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
||||||
* @param string $property Name of the property
|
* @param string $property Name of the property
|
||||||
* @param int $filter (optional) Filter of properties. Uses \ReflectionProperty class constants.
|
* @param int|null $filter (optional) Filter of properties. Uses \ReflectionProperty class constants.
|
||||||
* By default all properties are allowed / processed.
|
* By default all properties are allowed / processed.
|
||||||
* @return null|ReflectionProperty
|
* @return null|ReflectionProperty
|
||||||
*/
|
*/
|
||||||
@@ -512,7 +512,6 @@ class Reflection
|
|||||||
$properties = self::getProperties($className, $filter);
|
$properties = self::getProperties($className, $filter);
|
||||||
|
|
||||||
if (!empty($properties)) {
|
if (!empty($properties)) {
|
||||||
/** @var ReflectionProperty $reflectionProperty */
|
|
||||||
foreach ($properties as $reflectionProperty) {
|
foreach ($properties as $reflectionProperty) {
|
||||||
if ($reflectionProperty->getName() === $property) {
|
if ($reflectionProperty->getName() === $property) {
|
||||||
return $reflectionProperty;
|
return $reflectionProperty;
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ class ReflectionTest extends BaseTestCase
|
|||||||
public function testUsesTraitInvalidTrait($trait): void
|
public function testUsesTraitInvalidTrait($trait): void
|
||||||
{
|
{
|
||||||
$this->expectException(CannotResolveClassNameException::class);
|
$this->expectException(CannotResolveClassNameException::class);
|
||||||
self::assertNull(Reflection::usesTrait(DateTime::class, $trait));
|
Reflection::usesTrait(DateTime::class, $trait);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUsesTraitExistingClass(): void
|
public function testUsesTraitExistingClass(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user