ampache/ampache

View on GitHub
src/Repository/UserRepository.php

Summary

Maintainability
C
1 day
Test Coverage

The class UserRepository has an overall complexity of 54 which is very high. The configured complexity threshold is 50.
Open

final class UserRepository implements UserRepositoryInterface
{
    /**
     * This returns a built user from a rsstoken
     */
Severity: Minor
Found in src/Repository/UserRepository.php by phpmd

The class UserRepository has 16 public methods. Consider refactoring UserRepository to keep number of public methods under 10.
Open

final class UserRepository implements UserRepositoryInterface
{
    /**
     * This returns a built user from a rsstoken
     */
Severity: Minor
Found in src/Repository/UserRepository.php by phpmd

TooManyPublicMethods

Since: 0.1

A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

By default it ignores methods starting with 'get' or 'set'.

Example

Source https://phpmd.org/rules/codesize.html#toomanypublicmethods

UserRepository has 21 functions (exceeds 20 allowed). Consider refactoring.
Open

final class UserRepository implements UserRepositoryInterface
{
    /**
     * This returns a built user from a rsstoken
     */
Severity: Minor
Found in src/Repository/UserRepository.php - About 2 hrs to fix

    Function findByApiKey has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

        public function findByApiKey(string $apikey): ?User
        {
            if (!empty($apikey)) {
                // check for legacy unencrypted apikey
                $sql        = "SELECT `id` FROM `user` WHERE `apikey` = ?";
    Severity: Minor
    Found in src/Repository/UserRepository.php - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function findByStreamToken has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        public function findByStreamToken(string $streamToken): ?User
        {
            if (!empty($streamToken)) {
                // check for legacy unencrypted streamtoken
                $sql        = "SELECT `id` FROM `user` WHERE `streamtoken` = ?";
    Severity: Minor
    Found in src/Repository/UserRepository.php - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Avoid assigning values to variables in if clauses and the like (line '236', column '13').
    Open

        public function findByUsername(string $username): ?User
        {
            $user       = null;
            $sql        = 'SELECT `id` FROM `user` WHERE `username` = ?';
            $db_results = Dba::read($sql, array($username));
    Severity: Minor
    Found in src/Repository/UserRepository.php by phpmd

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    Avoid assigning values to variables in if clauses and the like (line '42', column '13').
    Open

        public function getByRssToken(string $rssToken): ?User
        {
            $user       = null;
            $sql        = "SELECT `id` FROM `user` WHERE `rsstoken` = ?";
            $db_results = Dba::read($sql, array($rssToken));
    Severity: Minor
    Found in src/Repository/UserRepository.php by phpmd

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    Avoid assigning values to variables in if clauses and the like (line '251', column '13').
    Open

        public function findByEmail(string $email): ?User
        {
            $user       = null;
            $sql        = 'SELECT `id` FROM `user` WHERE `email` = ?';
            $db_results = Dba::read($sql, array($email));
    Severity: Minor
    Found in src/Repository/UserRepository.php by phpmd

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    Avoid unused parameters such as '$userName'.
    Open

        public function updateStreamToken(int $userId, string $userName, string $streamToken): void
    Severity: Minor
    Found in src/Repository/UserRepository.php by phpmd

    UnusedFormalParameter

    Since: 0.2

    Avoid passing parameters to methods or constructors and then not using those parameters.

    Example

    class Foo
    {
        private function bar($howdy)
        {
            // $howdy is not used
        }
    }

    Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

    syntax error, unexpected end of file
    Open

    Severity: Critical
    Found in src/Repository/UserRepository.php by phan

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

                while ($row = Dba::fetch_assoc($db_results)) {
                    if ($row['streamtoken'] && $row['username']) {
                        $key        = hash('sha256', $row['streamtoken']);
                        $passphrase = hash('sha256', $row['username'] . $key);
                        if ($passphrase == $streamToken) {
    Severity: Major
    Found in src/Repository/UserRepository.php and 1 other location - About 1 hr to fix
    src/Repository/UserRepository.php on lines 306..314

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 103.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 2 locations. Consider refactoring.
    Open

                while ($row = Dba::fetch_assoc($db_results)) {
                    if ($row['apikey'] && $row['username']) {
                        $key        = hash('sha256', $row['apikey']);
                        $passphrase = hash('sha256', $row['username'] . $key);
                        if ($passphrase == $apikey) {
    Severity: Major
    Found in src/Repository/UserRepository.php and 1 other location - About 1 hr to fix
    src/Repository/UserRepository.php on lines 338..346

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 103.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 3 locations. Consider refactoring.
    Open

        public function getByRssToken(string $rssToken): ?User
        {
            $user       = null;
            $sql        = "SELECT `id` FROM `user` WHERE `rsstoken` = ?";
            $db_results = Dba::read($sql, array($rssToken));
    Severity: Minor
    Found in src/Repository/UserRepository.php and 2 other locations - About 30 mins to fix
    src/Repository/UserRepository.php on lines 231..241
    src/Repository/UserRepository.php on lines 246..256

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 90.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 3 locations. Consider refactoring.
    Open

        public function findByUsername(string $username): ?User
        {
            $user       = null;
            $sql        = 'SELECT `id` FROM `user` WHERE `username` = ?';
            $db_results = Dba::read($sql, array($username));
    Severity: Minor
    Found in src/Repository/UserRepository.php and 2 other locations - About 30 mins to fix
    src/Repository/UserRepository.php on lines 37..47
    src/Repository/UserRepository.php on lines 246..256

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 90.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Similar blocks of code found in 3 locations. Consider refactoring.
    Open

        public function findByEmail(string $email): ?User
        {
            $user       = null;
            $sql        = 'SELECT `id` FROM `user` WHERE `email` = ?';
            $db_results = Dba::read($sql, array($email));
    Severity: Minor
    Found in src/Repository/UserRepository.php and 2 other locations - About 30 mins to fix
    src/Repository/UserRepository.php on lines 37..47
    src/Repository/UserRepository.php on lines 231..241

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 90.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 37 and the first side effect is on line 32.
    Open

    <?php

    Possible parse error: class missing opening or closing brace
    Open

    final class UserRepository implements UserRepositoryInterface

    Possible parse error: class missing opening or closing brace
    Open

    final class UserRepository implements UserRepositoryInterface

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            foreach ($user_tables as $table_id) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function idByResetToken(string $token): int

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function findByUsername(string $username): ?User

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function getByRssToken(string $rssToken): ?User

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($result !== null) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function getValid(bool $includeDisabled = false): array

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function findByEmail(string $email): ?User

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function idByEmail(string $email): int

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if (User::is_cached($key, $value)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($username == '-1') {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            foreach ($user_tables as $table_id) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            while ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if (User::is_cached($key, $value)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            while ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function idByUsername(string $username): int

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            while ($row = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function collectGarbage(): void

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if ($token === $user_token) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if ($result !== null) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function getValidArray(bool $includeDisabled = false): array

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function findByWebsite(string $website): array

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                while ($row = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function getValidationByUsername(string $username): ?string

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (array_key_exists('id', $results)) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function enable(int $userId): void

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    if ($row['streamtoken'] && $row['username']) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function updateLastSeen(

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        if ($passphrase == $streamToken) {

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        if ($passphrase == $apikey) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function updateStreamToken(int $userId, string $userName, string $streamToken): void

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if (!empty($apikey)) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function updateRssToken(int $userId, string $rssToken): void

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (array_key_exists('username', $results)) {

    Line indented incorrectly; expected 16 spaces, found 20
    Open

                        }

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                while ($row = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            if (!empty($streamToken)) {

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function activateByUsername(string $username): void

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function updateApiKey(int $userId, string $apikey): void

    Line indented incorrectly; expected at least 8 spaces, found 4
    Open

        }

    Line indented incorrectly; expected at least 8 spaces, found 0
    Open

    }

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            while ($results = Dba::fetch_assoc($db_results)) {

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function findByStreamToken(string $streamToken): ?User

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 12 spaces, found 16
    Open

                    if ($row['apikey'] && $row['username']) {

    Line indented incorrectly; expected 4 spaces, found 8
    Open

            }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function getStatistics(int $timePeriod = 1200): array

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function findByApiKey(string $apikey): ?User

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                }

    Line indented incorrectly; expected 8 spaces, found 12
    Open

                if (array_key_exists('id', $results)) {

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        public function retrievePasswordFromUser(int $userId): string

    Line indented incorrectly; expected 0 spaces, found 4
    Open

        }

    There are no issues that match your filters.

    Category
    Status