private function detectProjectId(array $config)
    {
        $config += [
            'httpHandler' => null,
            'projectId' => null,
            'projectIdRequired' => false,
            'hasEmulator' => false,
            'preferNumericProjectId' => false,
            'suppressKeyFileNotice' => false
        ];

        if ($config['projectId']) {
            return $config['projectId'];
        }

        if ($config['hasEmulator']) {
            return 'emulator-project';
        }

        if (isset($config['keyFile'])) {
            if (isset($config['keyFile']['project_id'])) {
                return $config['keyFile']['project_id'];
            }

            if ($config['suppressKeyFileNotice'] !== true) {
                $serviceAccountUri = 'https://cloud.google.com/iam/docs/' .
                    'creating-managing-service-account-keys#creating_service_account_keys';

                trigger_error(
                    sprintf(
                        'A keyfile was given, but it does not contain a project ' .
                        'ID. This can indicate an old and obsolete keyfile, ' .
                        'in which case you should create a new one. To suppress ' .
                        'this message, set `suppressKeyFileNotice` to `true` in your client configuration. ' .
                        'To learn more about generating new keys, see this URL: %s',
                        $serviceAccountUri
                    ),
                    E_USER_NOTICE
                );
            }
        }

        if (getenv('GOOGLE_CLOUD_PROJECT')) {
            return getenv('GOOGLE_CLOUD_PROJECT');
        }

        if (getenv('GCLOUD_PROJECT')) {
            return getenv('GCLOUD_PROJECT');
        }

        if ($this->onGce($config['httpHandler'])) {
            $metadata = $this->getMetaData();
            $projectId = $config['preferNumericProjectId']
                ? $metadata->getNumericProjectId()
                : $metadata->getProjectId();
            if ($projectId) {
                return $projectId;
            }
        }

        if ($config['projectIdRequired']) {
            throw new GoogleException(
                'No project ID was provided, ' .
                'and we were unable to detect a default project ID.'
            );
        }
    }