Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Apply Doctrine Coding Standards 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Jun 13, 2018
1 parent 938a563 commit 42d4f76
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 89 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -19,7 +19,7 @@
"require-dev": {
"phpstan/phpstan": "^0.9.2",
"phpunit/phpunit": "^7.0",
"doctrine/coding-standard": "^1.0",
"doctrine/coding-standard": "^4.0",
"doctrine/common": "^2.8",
"squizlabs/php_codesniffer": "^3.0"
},
Expand Down
56 changes: 48 additions & 8 deletions composer.lock

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

2 changes: 0 additions & 2 deletions lib/Doctrine/Common/Reflection/ClassFinderInterface.php
Expand Up @@ -3,8 +3,6 @@

/**
* Finds a class in a PSR-0 structure.
*
* @author Karoly Negyesi <karoly@negyesi.net>
*/
interface ClassFinderInterface
{
Expand Down
29 changes: 18 additions & 11 deletions lib/Doctrine/Common/Reflection/Psr0FindFile.php
@@ -1,22 +1,27 @@
<?php
namespace Doctrine\Common\Reflection;

use const DIRECTORY_SEPARATOR;
use function is_file;
use function str_replace;
use function strpos;
use function strrpos;
use function substr;

/**
* Finds a class in a PSR-0 structure.
*
* @author Karoly Negyesi <karoly@negyesi.net>
*/
class Psr0FindFile implements ClassFinderInterface
{
/**
* The PSR-0 prefixes.
*
* @var array
* @var string[][]
*/
protected $prefixes;

/**
* @param array $prefixes An array of prefixes. Each key is a PHP namespace and each value is
* @param string[][] $prefixes An array of prefixes. Each key is a PHP namespace and each value is
* a list of directories.
*/
public function __construct($prefixes)
Expand All @@ -30,11 +35,11 @@ public function __construct($prefixes)
public function findFile($class)
{
$lastNsPos = strrpos($class, '\\');
if ('\\' == $class[0]) {
if ($class[0] === '\\') {
$class = substr($class, 1);
}

if (false !== $lastNsPos) {
if ($lastNsPos !== false) {
// namespaced class name
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $lastNsPos)) . DIRECTORY_SEPARATOR;
$className = substr($class, $lastNsPos + 1);
Expand All @@ -47,11 +52,13 @@ public function findFile($class)
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

foreach ($this->prefixes as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (is_file($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
if (strpos($class, $prefix) !== 0) {
continue;
}

foreach ($dirs as $dir) {
if (is_file($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
}
Expand Down
Expand Up @@ -7,8 +7,6 @@
/**
* PHP Runtime Reflection Public Property - special overrides for public properties.
*
* @author Marco Pivetta <ocramius@gmail.com>
* @since 2.4
*/
class RuntimePublicReflectionProperty extends ReflectionProperty
{
Expand All @@ -26,7 +24,7 @@ public function getValue($object = null)
if ($object instanceof Proxy && ! $object->__isInitialized()) {
$originalInitializer = $object->__getInitializer();
$object->__setInitializer(null);
$val = isset($object->$name) ? $object->$name : null;
$val = $object->$name ?? null;
$object->__setInitializer($originalInitializer);

return $val;
Expand All @@ -44,7 +42,7 @@ public function getValue($object = null)
*/
public function setValue($object, $value = null)
{
if ( ! ($object instanceof Proxy && ! $object->__isInitialized())) {
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
parent::setValue($object, $value);

return;
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/Common/Reflection/StaticReflectionClass.php
Expand Up @@ -13,9 +13,6 @@ class StaticReflectionClass extends ReflectionClass
*/
private $staticReflectionParser;

/**
* @param StaticReflectionParser $staticReflectionParser
*/
public function __construct(StaticReflectionParser $staticReflectionParser)
{
$this->staticReflectionParser = $staticReflectionParser;
Expand Down Expand Up @@ -46,7 +43,7 @@ public function getNamespaceName()
}

/**
* @return array
* @return string[]
*/
public function getUseStatements()
{
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/Common/Reflection/StaticReflectionMethod.php
Expand Up @@ -21,8 +21,7 @@ class StaticReflectionMethod extends ReflectionMethod
protected $methodName;

/**
* @param StaticReflectionParser $staticReflectionParser
* @param string $methodName
* @param string $methodName
*/
public function __construct(StaticReflectionParser $staticReflectionParser, $methodName)
{
Expand Down Expand Up @@ -71,7 +70,7 @@ public function getDocComment()
}

/**
* @return array
* @return string[]
*/
public function getUseStatements()
{
Expand Down
59 changes: 43 additions & 16 deletions lib/Doctrine/Common/Reflection/StaticReflectionParser.php
@@ -1,13 +1,33 @@
<?php

namespace Doctrine\Common\Reflection;

use Doctrine\Common\Annotations\TokenParser;
use ReflectionException;
use const T_CLASS;
use const T_DOC_COMMENT;
use const T_EXTENDS;
use const T_FUNCTION;
use const T_PAAMAYIM_NEKUDOTAYIM;
use const T_PRIVATE;
use const T_PROTECTED;
use const T_PUBLIC;
use const T_STRING;
use const T_USE;
use const T_VAR;
use const T_VARIABLE;
use function array_merge;
use function file_get_contents;
use function ltrim;
use function preg_match;
use function sprintf;
use function strpos;
use function strrpos;
use function strtolower;
use function substr;

/**
* Parses a file for namespaces/use/class declarations.
*
* @author Karoly Negyesi <karoly@negyesi.net>
*/
class StaticReflectionParser implements ReflectionProviderInterface
{
Expand All @@ -28,7 +48,7 @@ class StaticReflectionParser implements ReflectionProviderInterface
/**
* Whether the caller only wants class annotations.
*
* @var boolean.
* @var bool
*/
protected $classAnnotationOptimize;

Expand All @@ -42,7 +62,7 @@ class StaticReflectionParser implements ReflectionProviderInterface
/**
* Whether the parser has run.
*
* @var boolean
* @var bool
*/
protected $parsed = false;

Expand All @@ -56,7 +76,7 @@ class StaticReflectionParser implements ReflectionProviderInterface
/**
* The use statements of the class.
*
* @var array
* @var string[]
*/
protected $useStatements = [];

Expand All @@ -68,7 +88,7 @@ class StaticReflectionParser implements ReflectionProviderInterface
protected $docComment = [
'class' => '',
'property' => [],
'method' => []
'method' => [],
];

/**
Expand All @@ -90,8 +110,8 @@ class StaticReflectionParser implements ReflectionProviderInterface
*
* @param string $className The full, namespaced class name.
* @param ClassFinderInterface $finder A ClassFinder object which finds the class.
* @param boolean $classAnnotationOptimize Only retrieve the class docComment.
* Presumes there is only one statement per line.
* @param bool $classAnnotationOptimize Only retrieve the class docComment.
* Presumes there is only one statement per line.
*/
public function __construct($className, $finder, $classAnnotationOptimize = false)
{
Expand All @@ -114,13 +134,17 @@ public function __construct($className, $finder, $classAnnotationOptimize = fals
*/
protected function parse()
{
if ($this->parsed || ! $fileName = $this->finder->findFile($this->className)) {
$fileName = $this->finder->findFile($this->className);

if ($this->parsed || ! $fileName) {
return;
}
$this->parsed = true;
$contents = file_get_contents($fileName);
if ($this->classAnnotationOptimize) {
if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
$regex = sprintf('/\A.*^\s*((abstract|final)\s+)?class\s+%s\s+/sm', $this->shortClassName);

if (preg_match($regex, $contents, $matches)) {
$contents = $matches[0];
}
}
Expand Down Expand Up @@ -162,6 +186,7 @@ protected function parse()
// there can be & before the function name so find the
// string.
while (($token = $tokenParser->next()) && $token[0] !== T_STRING) {
continue;
}
$methodName = $token[1];
$this->docComment['method'][$methodName] = $docComment;
Expand All @@ -182,13 +207,15 @@ protected function parse()
$postfix = '';
}
foreach ($this->useStatements as $alias => $use) {
if ($alias == $prefix) {
$this->parentClassName = '\\' . $use . $postfix;
$fullySpecified = true;
if ($alias !== $prefix) {
continue;
}

$this->parentClassName = '\\' . $use . $postfix;
$fullySpecified = true;
}
}
if ( ! $fullySpecified) {
if (! $fullySpecified) {
$this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
}
break;
Expand Down Expand Up @@ -253,7 +280,7 @@ public function getReflectionProperty($propertyName)
/**
* Gets the use statements from this file.
*
* @return array
* @return string[]
*/
public function getUseStatements()
{
Expand Down Expand Up @@ -293,7 +320,7 @@ public function getStaticReflectionParserForDeclaringClass($type, $name)
if (isset($this->docComment[$type][$name])) {
return $this;
}
if ( ! empty($this->parentClassName)) {
if (! empty($this->parentClassName)) {
return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
}
throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
Expand Down

0 comments on commit 42d4f76

Please sign in to comment.