From bb2eb3f40d14dac76e31d8fba5e904a1ba5d1104 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 16 Nov 2017 13:32:46 +0100 Subject: [PATCH 1/4] add phpstan to travis --- .travis.yml | 10 ++++++---- Tests/Functional/Controller/ProfileControllerTest.php | 4 +++- phpstan.neon | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index c38ac23f..b7564917 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,25 +9,27 @@ cache: matrix: include: - php: 5.5 - # env: - # - COMPOSER_FLAGS="--prefer-lowest --prefer-dist --no-interaction" + env: + - COMPOSER_FLAGS="--prefer-dist --no-interaction" # --prefer-lowest not possible in sulu 1.3 - php: 7.0 env: - # - COMPOSER_FLAGS="--prefer-dist --no-interaction" + - COMPOSER_FLAGS="--prefer-dist --no-interaction" - CODE_COVERAGE=true + - PHPSTAN=true before_install: - phpenv config-add Tests/travis.php.ini - composer self-update install: - - travis_retry composer update $COMPOSER_FLAGS + - if [[ $PHPSTAN == 'true' ]]; then travis_retry composer require --dev phpstan/phpstan $COMPOSER_FLAGS ; else travis_retry composer update $COMPOSER_FLAGS ; fi - composer info -i - ./Tests/app/console doctrine:database:create - ./Tests/app/console doctrine:schema:update --force script: - ./vendor/bin/phpunit --coverage-clover=coverage.clover + - if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 1 -c phpstan.neon ; fi after_script: - if [[ $CODE_COVERAGE == 'true' ]]; then wget https://scrutinizer-ci.com/ocular.phar ; fi diff --git a/Tests/Functional/Controller/ProfileControllerTest.php b/Tests/Functional/Controller/ProfileControllerTest.php index 2bd79960..f73a28a6 100644 --- a/Tests/Functional/Controller/ProfileControllerTest.php +++ b/Tests/Functional/Controller/ProfileControllerTest.php @@ -88,7 +88,9 @@ public function testProfile() 'profile[contact][notes][0][value]' => 'Test', 'profile[contact][contactAddresses][0][main]' => 1, 'profile[_token]' => $crawler->filter('#profile__token')->first()->attr('value'), - ]); + ] + ); + $crawler = $client->submit($form); $this->assertHttpStatusCode(200, $client->getResponse()); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..409b6409 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + excludes_analyse: + - %currentWorkingDirectory%/vendor/* + From 16f74381b4218d6f9c7815c8c1964789edace97a Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 16 Nov 2017 14:24:45 +0100 Subject: [PATCH 2/4] fix phpstan errors --- EventListener/EmailConfirmationListener.php | 6 ++++++ Tests/Functional/Controller/ProfileControllerTest.php | 2 +- Tests/Functional/Controller/RegistrationTest.php | 2 +- composer.json | 1 + phpstan.neon | 1 + 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/EventListener/EmailConfirmationListener.php b/EventListener/EmailConfirmationListener.php index 53b52b90..feb26b41 100644 --- a/EventListener/EmailConfirmationListener.php +++ b/EventListener/EmailConfirmationListener.php @@ -18,6 +18,7 @@ use Sulu\Bundle\CommunityBundle\Event\CommunityEvent; use Sulu\Bundle\CommunityBundle\Mail\Mail; use Sulu\Bundle\CommunityBundle\Mail\MailFactoryInterface; +use Sulu\Bundle\SecurityBundle\Entity\User; use Sulu\Bundle\SecurityBundle\Util\TokenGeneratorInterface; /** @@ -72,6 +73,11 @@ public function __construct( public function sendConfirmationOnEmailChange(CommunityEvent $event) { $user = $event->getUser(); + + if (!$user instanceof User) { + throw new \RuntimeException('Community bundle user need to be instance uf Sulu User'); + } + if ($user->getEmail() === $user->getContact()->getMainEmail()) { return; } diff --git a/Tests/Functional/Controller/ProfileControllerTest.php b/Tests/Functional/Controller/ProfileControllerTest.php index f73a28a6..b6a2ea53 100644 --- a/Tests/Functional/Controller/ProfileControllerTest.php +++ b/Tests/Functional/Controller/ProfileControllerTest.php @@ -9,7 +9,7 @@ * with this source code in the file LICENSE. */ -namespace Functional\Controller; +namespace Sulu\Bundle\CommunityBundle\Tests\Functional\Controller; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; diff --git a/Tests/Functional/Controller/RegistrationTest.php b/Tests/Functional/Controller/RegistrationTest.php index 1c7b6774..f10887ad 100644 --- a/Tests/Functional/Controller/RegistrationTest.php +++ b/Tests/Functional/Controller/RegistrationTest.php @@ -9,7 +9,7 @@ * with this source code in the file LICENSE. */ -namespace Functional\Controller; +namespace Sulu\Bundle\CommunityBundle\Tests\Functional\Controller; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; diff --git a/composer.json b/composer.json index f1cd9502..54f32a97 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "jackalope/jackalope-doctrine-dbal": "^1.2.5", "symfony/monolog-bundle": "^2.8.7 || ^3.0", "massive/search-bundle": "@dev", + "massive/build-bundle": "^0.2", "zendframework/zend-stdlib": "~2.3", "zendframework/zendsearch": "@dev", "phpunit/phpunit": ">=4.8, <6.0" diff --git a/phpstan.neon b/phpstan.neon index 409b6409..7fcfb015 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,5 @@ parameters: excludes_analyse: - %currentWorkingDirectory%/vendor/* + - %currentWorkingDirectory%/Tests/* From 9759fba0322c6a23c1fd883d4acbc55034c49c9c Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 16 Nov 2017 15:01:56 +0100 Subject: [PATCH 3/4] try increase nesting for travis --- Tests/travis.php.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/travis.php.ini b/Tests/travis.php.ini index cc60274e..318faae7 100644 --- a/Tests/travis.php.ini +++ b/Tests/travis.php.ini @@ -1,2 +1,3 @@ memory_limit = 2048M +xdebug.max_nesting_level = 2048 From 2fdcd44f7f1acce62a8cb3140ba3eea54e60a802 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 16 Nov 2017 18:26:59 +0100 Subject: [PATCH 4/4] fix phpstan errors inside communitybundle and set phpstan to 4 --- .travis.yml | 2 +- Controller/ConfirmationController.php | 5 ++- Controller/ProfileController.php | 4 +- Entity/BlacklistItem.php | 12 +++--- Entity/BlacklistUser.php | 4 +- Entity/BlacklistUserRepository.php | 6 +-- Entity/EmailConfirmationTokenRepository.php | 12 +++--- EventListener/BlacklistListener.php | 4 +- EventListener/CompletionListener.php | 9 ++++- Manager/CommunityManager.php | 6 +-- Manager/CommunityManagerInterface.php | 10 ++--- Manager/UserManager.php | 42 ++++++++++++--------- Manager/UserManagerInterface.php | 6 +-- phpstan.neon | 1 + 14 files changed, 69 insertions(+), 54 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7564917..e97b5194 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ install: script: - ./vendor/bin/phpunit --coverage-clover=coverage.clover - - if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 1 -c phpstan.neon ; fi + - if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 4 -c phpstan.neon ; fi after_script: - if [[ $CODE_COVERAGE == 'true' ]]; then wget https://scrutinizer-ci.com/ocular.phar ; fi diff --git a/Controller/ConfirmationController.php b/Controller/ConfirmationController.php index 23f7cde0..486a8cfe 100644 --- a/Controller/ConfirmationController.php +++ b/Controller/ConfirmationController.php @@ -12,6 +12,7 @@ namespace Sulu\Bundle\CommunityBundle\Controller; use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; +use Sulu\Bundle\SecurityBundle\Entity\User; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -37,7 +38,9 @@ public function indexAction(Request $request, $token) $success = false; // Confirm user by token - if ($user = $communityManager->confirm($token)) { + $user = $communityManager->confirm($token); + + if ($user instanceof User) { // Save User $this->saveEntities(); diff --git a/Controller/ProfileController.php b/Controller/ProfileController.php index 5f2c7cb6..f3b5b6df 100644 --- a/Controller/ProfileController.php +++ b/Controller/ProfileController.php @@ -93,13 +93,13 @@ public function indexAction(Request $request) * @param User $user * @param string $locale * - * @return Media + * @return Media|null */ protected function saveAvatar(Form $form, User $user, $locale) { $uploadedFile = $form->get('contact')->get('avatar')->getData(); if (null === $uploadedFile) { - return; + return null; } $systemCollectionManager = $this->get('sulu_media.system_collections.manager'); diff --git a/Entity/BlacklistItem.php b/Entity/BlacklistItem.php index b46dced0..4f500506 100644 --- a/Entity/BlacklistItem.php +++ b/Entity/BlacklistItem.php @@ -30,17 +30,17 @@ class BlacklistItem private $id; /** - * @var string + * @var string|null */ private $pattern; /** - * @var string + * @var string|null */ private $regexp; /** - * @var string + * @var string|null */ private $type; @@ -70,7 +70,7 @@ public function getId() /** * Get pattern. * - * @return string + * @return string|null */ public function getPattern() { @@ -95,7 +95,7 @@ public function setPattern($pattern) /** * Get regexp. * - * @return string + * @return string|null */ public function getRegexp() { @@ -105,7 +105,7 @@ public function getRegexp() /** * Get type. * - * @return string + * @return string|null */ public function getType() { diff --git a/Entity/BlacklistUser.php b/Entity/BlacklistUser.php index 42bf7359..31c28421 100644 --- a/Entity/BlacklistUser.php +++ b/Entity/BlacklistUser.php @@ -28,7 +28,7 @@ class BlacklistUser private $id; /** - * @var string + * @var string|null */ private $token; @@ -74,7 +74,7 @@ public function getId() /** * Returns token. * - * @return string + * @return string|null */ public function getToken() { diff --git a/Entity/BlacklistUserRepository.php b/Entity/BlacklistUserRepository.php index 04b4397a..785ab6f8 100644 --- a/Entity/BlacklistUserRepository.php +++ b/Entity/BlacklistUserRepository.php @@ -22,16 +22,16 @@ class BlacklistUserRepository extends EntityRepository * * @param string $token * - * @return BlacklistUser + * @return BlacklistUser|null */ public function findByToken($token) { try { return $this->findOneBy(['token' => $token]); } catch (NonUniqueResultException $e) { - return; + return null; } catch (NoResultException $e) { - return; + return null; } } } diff --git a/Entity/EmailConfirmationTokenRepository.php b/Entity/EmailConfirmationTokenRepository.php index 7ad7ec4f..00623411 100644 --- a/Entity/EmailConfirmationTokenRepository.php +++ b/Entity/EmailConfirmationTokenRepository.php @@ -26,16 +26,16 @@ class EmailConfirmationTokenRepository extends EntityRepository * * @param string $token * - * @return EmailConfirmationToken + * @return EmailConfirmationToken|null */ public function findByToken($token) { try { return $this->findOneBy(['token' => $token]); } catch (NonUniqueResultException $e) { - return; + return null; } catch (NoResultException $e) { - return; + return null; } } @@ -44,16 +44,16 @@ public function findByToken($token) * * @param UserInterface $user * - * @return EmailConfirmationToken + * @return EmailConfirmationToken|null */ public function findByUser($user) { try { return $this->findOneBy(['user' => $user]); } catch (NonUniqueResultException $e) { - return; + return null; } catch (NoResultException $e) { - return; + return null; } } } diff --git a/EventListener/BlacklistListener.php b/EventListener/BlacklistListener.php index 3fbd5a50..ca5f9d09 100644 --- a/EventListener/BlacklistListener.php +++ b/EventListener/BlacklistListener.php @@ -101,14 +101,14 @@ public function validateEmail(CommunityEvent $event) * * @param string $email * - * @return string + * @return string|null */ private function getType($email) { $items = $this->blacklistItemRepository->findBySender($email); if (0 === count($items)) { - return; + return null; } foreach ($items as $item) { diff --git a/EventListener/CompletionListener.php b/EventListener/CompletionListener.php index bf0d102b..b5e06e0a 100644 --- a/EventListener/CompletionListener.php +++ b/EventListener/CompletionListener.php @@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; /** * Validates the current user entity. @@ -93,6 +94,10 @@ public function onRequest(GetResponseEvent $event) $token = $this->tokenStorage->getToken(); + if (!$token instanceof TokenInterface) { + return; + } + /** @var User $user */ $user = $token->getUser(); @@ -131,12 +136,12 @@ public function addValidator(CompletionInterface $validator, $webspaceKey) /** * @param string $webspaceKey * - * @return CompletionInterface + * @return CompletionInterface|null */ protected function getValidator($webspaceKey) { if (!isset($this->validators[$webspaceKey])) { - return; + return null; } return $this->validators[$webspaceKey]; diff --git a/Manager/CommunityManager.php b/Manager/CommunityManager.php index 5a76bf7f..17957b33 100644 --- a/Manager/CommunityManager.php +++ b/Manager/CommunityManager.php @@ -161,7 +161,7 @@ public function completion(User $user) public function login(User $user, Request $request) { if (!$user->getEnabled()) { - return; + return null; } $token = new UsernamePasswordToken( @@ -187,7 +187,7 @@ public function confirm($token) $user = $this->userManager->findByConfirmationKey($token); if (!$user) { - return; + return null; } // Remove Confirmation Key @@ -209,7 +209,7 @@ public function passwordForget($emailUsername) $user = $this->userManager->findUser($emailUsername); if (!$user) { - return; + return null; } $user->setPasswordResetToken($this->userManager->getUniqueToken('passwordResetToken')); diff --git a/Manager/CommunityManagerInterface.php b/Manager/CommunityManagerInterface.php index 6075092d..eddd7d18 100644 --- a/Manager/CommunityManagerInterface.php +++ b/Manager/CommunityManagerInterface.php @@ -52,7 +52,7 @@ public function completion(User $user); * @param User $user * @param Request $request * - * @return UsernamePasswordToken + * @return UsernamePasswordToken|null */ public function login(User $user, Request $request); @@ -61,7 +61,7 @@ public function login(User $user, Request $request); * * @param string $token * - * @return User + * @return User|null */ public function confirm($token); @@ -70,14 +70,14 @@ public function confirm($token); * * @param string $emailUsername * - * @return User + * @return User|null */ public function passwordForget($emailUsername); /** * Reset user password token. * - * @param User $user + * @param User $user|null * * @return User */ @@ -107,7 +107,7 @@ public function getConfigProperty($property); * @param string $type * @param string $property * - * @return string + * @return mixed * * @throws \Exception */ diff --git a/Manager/UserManager.php b/Manager/UserManager.php index 276a7fd6..5f72f254 100644 --- a/Manager/UserManager.php +++ b/Manager/UserManager.php @@ -12,16 +12,16 @@ namespace Sulu\Bundle\CommunityBundle\Manager; use Doctrine\ORM\EntityManagerInterface; -use Sulu\Bundle\ContactBundle\Contact\ContactManager; -use Sulu\Bundle\ContactBundle\Entity\ContactRepository; +use Sulu\Bundle\ContactBundle\Contact\ContactManagerInterface; use Sulu\Bundle\ContactBundle\Entity\Email; use Sulu\Bundle\ContactBundle\Entity\EmailType; -use Sulu\Bundle\SecurityBundle\Entity\RoleRepository; use Sulu\Bundle\SecurityBundle\Entity\User; -use Sulu\Bundle\SecurityBundle\Entity\UserRepository; use Sulu\Bundle\SecurityBundle\Entity\UserRole; use Sulu\Bundle\SecurityBundle\Util\TokenGeneratorInterface; +use Sulu\Component\Contact\Model\ContactRepositoryInterface; use Sulu\Component\Security\Authentication\RoleInterface; +use Sulu\Component\Security\Authentication\RoleRepositoryInterface; +use Sulu\Component\Security\Authentication\UserRepositoryInterface; use Sulu\Component\Webspace\Manager\WebspaceManagerInterface; /** @@ -45,22 +45,22 @@ class UserManager implements UserManagerInterface protected $tokenGenerator; /** - * @var UserRepository + * @var UserRepositoryInterface */ protected $userRepository; /** - * @var RoleRepository + * @var RoleRepositoryInterface */ protected $roleRepository; /** - * @var ContactRepository + * @var ContactRepositoryInterface */ protected $contactRepository; /** - * @var ContactRepository + * @var ContactManagerInterface */ protected $contactManager; @@ -70,19 +70,19 @@ class UserManager implements UserManagerInterface * @param EntityManagerInterface $entityManager * @param WebspaceManagerInterface $webspaceManager * @param TokenGeneratorInterface $tokenGenerator - * @param UserRepository $userRepository - * @param RoleRepository $roleRepository - * @param ContactRepository $contactRepository - * @param ContactManager $contactManager + * @param UserRepositoryInterface $userRepository + * @param RoleRepositoryInterface $roleRepository + * @param ContactRepositoryInterface $contactRepository + * @param ContactManagerInterface $contactManager */ public function __construct( EntityManagerInterface $entityManager, WebspaceManagerInterface $webspaceManager, TokenGeneratorInterface $tokenGenerator, - UserRepository $userRepository, - RoleRepository $roleRepository, - ContactRepository $contactRepository, - ContactManager $contactManager + UserRepositoryInterface $userRepository, + RoleRepositoryInterface $roleRepository, + ContactRepositoryInterface $contactRepository, + ContactManagerInterface $contactManager ) { $this->entityManager = $entityManager; $this->webspaceManager = $webspaceManager; @@ -186,7 +186,7 @@ public function findByPasswordResetToken($token) $user = $this->userRepository->findOneBy(['passwordResetToken' => $token]); if (!$user || $user->getPasswordResetTokenExpiresAt() < new \DateTime()) { - return; + return null; } return $user; @@ -205,6 +205,12 @@ public function findByConfirmationKey($token) */ public function findUser($identifier) { - return $this->userRepository->findUserByIdentifier($identifier); + $user = $this->userRepository->findUserByIdentifier($identifier); + + if (!$user instanceof User) { + return null; + } + + return $user; } } diff --git a/Manager/UserManagerInterface.php b/Manager/UserManagerInterface.php index ec1c3bad..a2463d5f 100644 --- a/Manager/UserManagerInterface.php +++ b/Manager/UserManagerInterface.php @@ -43,7 +43,7 @@ public function getUniqueToken($field); * * @param string $token * - * @return User + * @return User|null */ public function findByPasswordResetToken($token); @@ -52,7 +52,7 @@ public function findByPasswordResetToken($token); * * @param string $token * - * @return User + * @return User|null */ public function findByConfirmationKey($token); @@ -61,7 +61,7 @@ public function findByConfirmationKey($token); * * @param string $identifier * - * @return User + * @return User|null */ public function findUser($identifier); } diff --git a/phpstan.neon b/phpstan.neon index 7fcfb015..6147fb74 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,6 @@ parameters: excludes_analyse: + - %currentWorkingDirectory%/DependencyInjection/Configuration.php - %currentWorkingDirectory%/vendor/* - %currentWorkingDirectory%/Tests/*