Skip to content
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

[stable10] owncloud coding standard #31657

Closed
wants to merge 12 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 13 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ pipeline:
matrix:
TEST_SUITE: lint

php-cs-fixer:
image: owncloudci/php:${PHP_VERSION}
pull: true
commands:
- make test-php-style
when:
matrix:
TEST_SUITE: php-cs-fixer

phpunit:
image: owncloudci/php:${PHP_VERSION}
pull: true
Expand Down Expand Up @@ -394,6 +403,10 @@ matrix:
# TEST_SUITE: phpunit


# php-cs-fixer
- TEST_SUITE: php-cs-fixer
PHP_VERSION: 7.2

# Litmus
- PHP_VERSION: 7.1
USE_SERVER: true
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ Vagrantfile
/config/autoconfig.php
clover.xml
/tests/output

.php_cs.cache
42 changes: 42 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

$dirToParse = 'apps';
$dirIterator = new DirectoryIterator(__DIR__ . '/' . $dirToParse);

$bundledApps = [
'comments',
'dav',
'federatedfilesharing',
'federation',
'files',
'files_external',
'files_sharing',
'files_trashbin',
'files_versions',
'provisioning_api',
'systemtags',
'testing',
'updatenotification'
];

$excludeDirs = [
'lib/composer',
'build',
'apps/files_external/3rdparty',
'config'
];

foreach ($dirIterator as $fileinfo) {
$filename = $fileinfo->getFilename();
if ($fileinfo->isDir() && !$fileinfo->isDot() && !in_array($filename, $bundledApps)) {
$excludeDirs[] = $dirToParse . '/' . $filename;
}
}

$finder = PhpCsFixer\Finder::create()
->exclude($excludeDirs)
->in(__DIR__);

$config = new OC\CodingStandard\Config();
$config->setFinder($finder);
return $config;
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ test-acceptance: $(composer_dev_deps)
test-php-lint: $(composer_dev_deps)
$(composer_deps)/bin/parallel-lint --exclude lib/composer --exclude build .

.PHONY: test-php-style
test-php-style: $(composer_dev_deps)
$(composer_deps)/bin/php-cs-fixer fix -v --diff --diff-format udiff --dry-run --allow-risky yes

.PHONY: test
test: test-php-lint test-php test-js test-acceptance
test: test-php-lint test-php-style test-php test-js test-acceptance

.PHONY: clean-test-acceptance
clean-test-acceptance:
Expand Down
12 changes: 6 additions & 6 deletions apps/comments/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener(
'OCA\Files::loadAdditionalScripts',
function() {
function () {
\OCP\Util::addScript('oc-backbone-webdav');
\OCP\Util::addScript('comments', 'app');
\OCP\Util::addScript('comments', 'commentmodel');
Expand All @@ -37,14 +37,14 @@ function() {
);

$activityManager = \OC::$server->getActivityManager();
$activityManager->registerExtension(function() {
$activityManager->registerExtension(function () {
$application = new \OCP\AppFramework\App('comments');
/** @var \OCA\Comments\Activity\Extension $extension */
$extension = $application->getContainer()->query('OCA\Comments\Activity\Extension');
return $extension;
});

$managerListener = function(\OCP\Comments\CommentsEvent $event) use ($activityManager) {
$managerListener = function (\OCP\Comments\CommentsEvent $event) use ($activityManager) {
$application = new \OCP\AppFramework\App('comments');
/** @var \OCA\Comments\Activity\Listener $listener */
$listener = $application->getContainer()->query('OCA\Comments\Activity\Listener');
Expand All @@ -53,9 +53,9 @@ function() {

$eventDispatcher->addListener(\OCP\Comments\CommentsEvent::EVENT_ADD, $managerListener);

$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function(\OCP\Comments\CommentsEntityEvent $event) {
$event->addEntityCollection('files', function($name) {
$nodes = \OC::$server->getUserFolder()->getById(intval($name));
$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function (\OCP\Comments\CommentsEntityEvent $event) {
$event->addEntityCollection('files', function ($name) {
$nodes = \OC::$server->getUserFolder()->getById(\intval($name));
return !empty($nodes);
});
});
10 changes: 4 additions & 6 deletions apps/comments/lib/Activity/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public function translate($app, $text, $params, $stripPath, $highlightParams, $l
* @return bool|string
*/
protected function translateShort($text, IL10N $l, array $params) {

switch ($text) {
case self::ADD_COMMENT_SUBJECT:
if ($this->authorIsCurrentUser($params[0])) {
Expand All @@ -172,7 +171,6 @@ protected function translateShort($text, IL10N $l, array $params) {
* @return bool|string
*/
protected function translateLong($text, IL10N $l, array $params) {

switch ($text) {
case self::ADD_COMMENT_SUBJECT:
if ($this->authorIsCurrentUser($params[0])) {
Expand All @@ -194,7 +192,7 @@ protected function translateLong($text, IL10N $l, array $params) {
*/
protected function authorIsCurrentUser($user) {
try {
return strip_tags($user) === $this->activityManager->getCurrentUserId();
return \strip_tags($user) === $this->activityManager->getCurrentUserId();
} catch (\UnexpectedValueException $e) {
return false;
}
Expand Down Expand Up @@ -279,7 +277,7 @@ public function filterNotificationTypes($types, $filter) {
if ($filter === self::APP_NAME) {
return [self::APP_NAME];
}
if (in_array($filter, ['all', 'by', 'self', 'filter'])) {
if (\in_array($filter, ['all', 'by', 'self', 'filter'])) {
$types[] = self::APP_NAME;
return $types;
}
Expand All @@ -304,11 +302,11 @@ public function getQueryForFilter($filter) {
* @return string
*/
protected function convertParameterToComment($parameter) {
if (preg_match('/^\<parameter\>(\d*)\<\/parameter\>$/', $parameter, $matches)) {
if (\preg_match('/^\<parameter\>(\d*)\<\/parameter\>$/', $parameter, $matches)) {
try {
$comment = $this->commentsManager->get((int) $matches[1]);
$message = $comment->getMessage();
$message = str_replace("\n", '<br />', str_replace(['<', '>'], ['&lt;', '&gt;'], $message));
$message = \str_replace("\n", '<br />', \str_replace(['<', '>'], ['&lt;', '&gt;'], $message));
return $message;
} catch (NotFoundException $e) {
return '';
Expand Down
10 changes: 5 additions & 5 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(IManager $activityManager,
*/
public function commentEvent(CommentsEvent $event) {
if ($event->getComment()->getObjectType() !== 'files'
|| !in_array($event->getEvent(), [CommentsEvent::EVENT_ADD])
|| !\in_array($event->getEvent(), [CommentsEvent::EVENT_ADD])
|| !$this->appManager->isInstalled('activity')) {
// Comment not for file, not adding a comment or no activity-app enabled (save the energy)
return;
Expand All @@ -89,13 +89,13 @@ public function commentEvent(CommentsEvent $event) {
$nodes = $ownerFolder->getById($event->getComment()->getObjectId());
if (!empty($nodes)) {
/** @var Node $node */
$node = array_shift($nodes);
$node = \array_shift($nodes);
$path = $node->getPath();
if (strpos($path, '/' . $owner . '/files/') === 0) {
$path = substr($path, strlen('/' . $owner . '/files'));
if (\strpos($path, '/' . $owner . '/files/') === 0) {
$path = \substr($path, \strlen('/' . $owner . '/files'));
}
// Get all users that have access to the mount point
$users = array_merge($users, Share::getUsersSharingFile($path, $owner, true, true));
$users = \array_merge($users, Share::getUsersSharingFile($path, $owner, true, true));
}
}

Expand Down
45 changes: 22 additions & 23 deletions apps/comments/lib/Dav/CommentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\Comments\Dav;


use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\MessageTooLongException;
Expand Down Expand Up @@ -79,12 +78,12 @@ public function __construct(
$this->comment = $comment;
$this->logger = $logger;

$methods = get_class_methods($this->comment);
$methods = array_filter($methods, function($name){
return strpos($name, 'get') === 0;
$methods = \get_class_methods($this->comment);
$methods = \array_filter($methods, function ($name) {
return \strpos($name, 'get') === 0;
});
foreach($methods as $getter) {
$name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3));
foreach ($methods as $getter) {
$name = '{'.self::NS_OWNCLOUD.'}' . \lcfirst(\substr($getter, 3));
$this->properties[$name] = $getter;
}
$this->userManager = $userManager;
Expand All @@ -96,7 +95,7 @@ public function __construct(
*
* @return array
*/
static public function getPropertyNames() {
public static function getPropertyNames() {
return [
'{http://owncloud.org/ns}id',
'{http://owncloud.org/ns}parentId',
Expand All @@ -118,8 +117,8 @@ static public function getPropertyNames() {

protected function checkWriteAccessOnComment() {
$user = $this->userSession->getUser();
if( $this->comment->getActorType() !== 'users'
|| is_null($user)
if ($this->comment->getActorType() !== 'users'
|| $user === null
|| $this->comment->getActorId() !== $user->getUID()
) {
throw new Forbidden('Only authors are allowed to edit their comment.');
Expand All @@ -131,7 +130,7 @@ protected function checkWriteAccessOnComment() {
*
* @return void
*/
function delete() {
public function delete() {
$this->checkWriteAccessOnComment();
$this->commentsManager->delete($this->comment->getId());
}
Expand All @@ -143,7 +142,7 @@ function delete() {
*
* @return string
*/
function getName() {
public function getName() {
return $this->comment->getId();
}

Expand All @@ -153,7 +152,7 @@ function getName() {
* @param string $name The new name
* @throws MethodNotAllowed
*/
function setName($name) {
public function setName($name) {
throw new MethodNotAllowed();
}

Expand All @@ -162,7 +161,7 @@ function setName($name) {
*
* @return int
*/
function getLastModified() {
public function getLastModified() {
return null;
}

Expand All @@ -182,7 +181,7 @@ public function updateComment($propertyValue) {
return true;
} catch (\Exception $e) {
$this->logger->logException($e, ['app' => 'dav/comments']);
if($e instanceof MessageTooLongException) {
if ($e instanceof MessageTooLongException) {
$msg = 'Message exceeds allowed character limit of ';
throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e);
}
Expand All @@ -202,7 +201,7 @@ public function updateComment($propertyValue) {
* @param PropPatch $propPatch
* @return void
*/
function propPatch(PropPatch $propPatch) {
public function propPatch(PropPatch $propPatch) {
// other properties than 'message' are read only
$propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']);
}
Expand All @@ -222,32 +221,32 @@ function propPatch(PropPatch $propPatch) {
* @param array $properties
* @return array
*/
function getProperties($properties) {
$properties = array_keys($this->properties);
public function getProperties($properties) {
$properties = \array_keys($this->properties);

$result = [];
foreach($properties as $property) {
foreach ($properties as $property) {
$getter = $this->properties[$property];
if(method_exists($this->comment, $getter)) {
if (\method_exists($this->comment, $getter)) {
$result[$property] = $this->comment->$getter();
}
}

if($this->comment->getActorType() === 'users') {
if ($this->comment->getActorType() === 'users') {
$user = $this->userManager->get($this->comment->getActorId());
$displayName = is_null($user) ? null : $user->getDisplayName();
$displayName = $user === null ? null : $user->getDisplayName();
$result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName;
}

$unread = null;
$user = $this->userSession->getUser();
if(!is_null($user)) {
if ($user !== null) {
$readUntil = $this->commentsManager->getReadMark(
$this->comment->getObjectType(),
$this->comment->getObjectId(),
$user
);
if(is_null($readUntil)) {
if ($readUntil === null) {
$unread = 'true';
} else {
$unread = $this->comment->getCreationDateTime() > $readUntil;
Expand Down
Loading